在Xcode IDE中初始化我自己的c++类向量
Initializing a vector of my own class in C++ in Xcode IDE
我目前遇到的问题如下:
我试图在我的main.cpp文件中构造一个只包含SingleBox对象的向量(SingleBox是我定义的一个类),然而,当我试图填充或调整该向量的大小时,向量从不包含任何SingleBox对象,并且下面尝试填充或调整向量大小的代码从未在我的main.cpp文件中执行。
提前向任何提供帮助解决此问题的人表示感谢。
文件main.cpp
#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;
}
文件SingleBox.h
#ifndef __Sudoku_Solver__SingleBox__
#define __Sudoku_Solver__SingleBox__
#include <iostream>
#include <vector>
// SingleBox class definition
class SingleBox
{
public:
SingleBox( int nineBoxNum, int boxValue ); // constructor
void updatePoss( int number ); // function to remove disproven possibilities
// get functions
bool isComplete( ); // function to get the completion indicator
int getBoxValue( ); // function to get the boxValue
// debugging functions
void debugPrinter( );
private:
bool completionStatus; // if the value of the SingleBox is non-zero
int nineBoxNumber; // the NineBox to which the SingleBox belongs
int boxValue; // the value of the SingleBox
std::vector< int > possibilities; // possible values for the SingleBox
// set functions
void setNineBoxNum( int nineBoxNum ); // function to set the NineBox number
void setBoxValue( int boxVal ); // function to set the boxValue
void setIndicator( bool isComplete ); // function to set the completion indicator
};
#endif /* defined(__Sudoku_Solver__SingleBox__) */
文件SingleBox.cpp
#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
由于设置了断点,Xcode IDE没有运行我的部分代码。通过g++编译运行代码验证了我的代码是可操作的,并且从Xcode中删除断点允许我的代码在IDE中运行。
相关文章:
- 没有为自己的结构调用列表推回方法
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- C++从对象自己的类中删除对象
- 使用 std::optional,而不是自己的结构
- 子轴围绕父轴而不是他自己的轴旋转
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++ 如何为自己的迭代器类从迭代器转换为const_iterator?
- 重载 + 自己的类和 std::string 的运算符
- 类无法访问自己的私有静态 constexpr 方法 - Clang bug?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何访问模板参数自己的模板参数?
- 将矩阵乘以我自己的输入的向量
- 您应该在什么时候创建自己的异常类型
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 如何正确包含我自己的标头?
- 自己的自定义向量类. 内存重新分配
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- 如何使用ZeroMQ为协议缓冲区编写自己的RPC实现