C++ 在C+中初始化我自己的类的向量+;在Xcode IDE中
我目前遇到的问题如下: 我试图在main.cpp文件中构造一个只包含SingleBox对象的向量(SingleBox是我定义的一个类),但是,当我试图填充或调整该向量的大小时,该向量从不包含任何SingleBox对象,并且在my main.cpp文件中从未执行填充或调整向量大小的尝试下面的代码 提前感谢您为解决此问题提供帮助的任何人 文件main.cppC++ 在C+中初始化我自己的类的向量+;在Xcode IDE中,c++,xcode,vector,C++,Xcode,Vector,我目前遇到的问题如下: 我试图在main.cpp文件中构造一个只包含SingleBox对象的向量(SingleBox是我定义的一个类),但是,当我试图填充或调整该向量的大小时,该向量从不包含任何SingleBox对象,并且在my main.cpp文件中从未执行填充或调整向量大小的尝试下面的代码 提前感谢您为解决此问题提供帮助的任何人 文件main.cpp #include <iostream> #include "SingleBox.h" #include "NineBox.h"
#include <iostream>
#include "SingleBox.h"
#include "NineBox.h"
int main( int argc, const char * argv[] )
{
std::vector< SingleBox > vec;
std::cout << "Below is the attempt to populate: \n";
vec.push_back( SingleBox( 0, 0 ) );
std::cout << "Nothing from this point onward executes...\n";
std::cout << "The size: " << vec.size() << std::endl;
return 0;
}
#include "SingleBox.h"
// SingleBox constructor initializes each data member
SingleBox::SingleBox( int nineBoxNum, int boxVal )
{
setNineBoxNum( nineBoxNum );
setBoxValue( boxVal );
setIndicator( false );
possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9};
} // end SingleBox constructor
// set new NineBox number
void SingleBox::setNineBoxNum( int nineBoxNum )
{
nineBoxNumber = nineBoxNum;
} // end function setNineBoxNum
// set new BoxValue number
void SingleBox::setBoxValue( int boxVal )
{
boxValue = boxVal;
} // end function setBoxValue
// set new completion indicator
void SingleBox::setIndicator( bool isComplete )
{
completionStatus = isComplete;
} // end function setIndicator
// return the status indicator
bool SingleBox::isComplete( )
{
return completionStatus;
}
// return the BoxValue
int SingleBox::getBoxValue( )
{
return boxValue;
} // end function getBoxValue
// set value to zero if in possibilities vector
void SingleBox::updatePoss( int number )
{
int zeroCnt = 0;
int value = 0;
for ( int i = 0; i < possibilities.size(); i++ ) {
if ( possibilities[ i ] == number ) {
possibilities[ i ] = 0;
break;
}
}
for ( int j = 0; j < possibilities.size(); j++ ) {
if ( possibilities[ j ] == 0 ) {
zeroCnt++;
}
else {
value = possibilities[ j ];
}
}
if ( zeroCnt == possibilities.size() - 1 ) {
boxValue = value;
setIndicator( true );
}
} // end function updateProbs
// print all data members of the class to the console
void SingleBox::debugPrinter( )
{
std::cout << std::endl;
std::cout << "SingleBox class contains the following \n";
std::cout << " - Completion status = " << completionStatus << std::endl;
std::cout << " - NineBoxNumber = " << nineBoxNumber << std::endl;
std::cout << " - BoxValue = " << boxValue << std::endl;
std::cout << " - Possibilities = [";
for (int i = 0; i < possibilities.size(); i++) {
std::cout << possibilities[i];
if (i != possibilities.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]\n" << std::endl;
} // end function debugPrinter
#包括
#包括“SingleBox.h”
#包括“NineBox.h”
int main(int argc,const char*argv[]
{
标准::向量vec;
std::coutXcode IDE没有运行我的部分代码,因为设置了断点。通过g++编译运行代码验证了我的代码是可操作的,从Xcode中删除断点允许我的代码在IDE中操作。你说下面的代码永远不会执行是什么意思?发生了什么?编译中有错误吗er?XCode是否会立即让您进入调试模式,尝试找出哪里出了问题(AKAgdb
)?请阅读Xcode并不表示任何错误或警告。在尝试初始化向量后,它只是不向控制台输出任何消息。我复制了您的程序,将其粘贴到单个文件中,然后编译并执行它:它似乎对我来说运行良好。您是否尝试在IDE之外对其进行编译?不,我没有尝试使用g++编译器。但是,我会继续,并尝试这样做,因为这是有效的一点。
#include "SingleBox.h"
// SingleBox constructor initializes each data member
SingleBox::SingleBox( int nineBoxNum, int boxVal )
{
setNineBoxNum( nineBoxNum );
setBoxValue( boxVal );
setIndicator( false );
possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9};
} // end SingleBox constructor
// set new NineBox number
void SingleBox::setNineBoxNum( int nineBoxNum )
{
nineBoxNumber = nineBoxNum;
} // end function setNineBoxNum
// set new BoxValue number
void SingleBox::setBoxValue( int boxVal )
{
boxValue = boxVal;
} // end function setBoxValue
// set new completion indicator
void SingleBox::setIndicator( bool isComplete )
{
completionStatus = isComplete;
} // end function setIndicator
// return the status indicator
bool SingleBox::isComplete( )
{
return completionStatus;
}
// return the BoxValue
int SingleBox::getBoxValue( )
{
return boxValue;
} // end function getBoxValue
// set value to zero if in possibilities vector
void SingleBox::updatePoss( int number )
{
int zeroCnt = 0;
int value = 0;
for ( int i = 0; i < possibilities.size(); i++ ) {
if ( possibilities[ i ] == number ) {
possibilities[ i ] = 0;
break;
}
}
for ( int j = 0; j < possibilities.size(); j++ ) {
if ( possibilities[ j ] == 0 ) {
zeroCnt++;
}
else {
value = possibilities[ j ];
}
}
if ( zeroCnt == possibilities.size() - 1 ) {
boxValue = value;
setIndicator( true );
}
} // end function updateProbs
// print all data members of the class to the console
void SingleBox::debugPrinter( )
{
std::cout << std::endl;
std::cout << "SingleBox class contains the following \n";
std::cout << " - Completion status = " << completionStatus << std::endl;
std::cout << " - NineBoxNumber = " << nineBoxNumber << std::endl;
std::cout << " - BoxValue = " << boxValue << std::endl;
std::cout << " - Possibilities = [";
for (int i = 0; i < possibilities.size(); i++) {
std::cout << possibilities[i];
if (i != possibilities.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]\n" << std::endl;
} // end function debugPrinter