C++ 我是否正确访问此向量变量?

C++ 我是否正确访问此向量变量?,c++,vector,segmentation-fault,std,declaration,C++,Vector,Segmentation Fault,Std,Declaration,我试图找出我的代码在哪里存在seg错误,我认为这可能与我如何访问下面函数中的变量有关: /**************************************************************** * Function for getting the value of a square. **/ int Board::getSquare(int row, int col) { vector<int> rowVector = this->theBoar

我试图找出我的代码在哪里存在seg错误,我认为这可能与我如何访问下面函数中的变量有关:

/****************************************************************
 * Function for getting the value of a square.
**/
int Board::getSquare(int row, int col)
{
  vector<int> rowVector = this->theBoard[row];
//gets desired row from theBoard
  return rowVector[col];
//returns desired column of the row from theBoard
} // int Board::getSquare(int row, int col)
/****************************************************************
*用于获取正方形值的函数。
**/
内部板::getSquare(内部行,内部列)
{
向量行向量=此->电路板[行];
//从黑板上获取所需的行
返回行向量[col];
//从线路板返回所需的行列
}//int Board::getSquare(int行,int列)
董事会是班级董事会的一个私有变量:

private:
/****************************************************************
 * Variables.
**/
  vector< vector<int> > theBoard;
private:
/****************************************************************
*变量。
**/
向量theBoard;

我是否需要单独声明和启动rowVector变量?如果是这样,我该怎么做?

您应该检查大小或使用
.at
访问您不确定的变量,即:

if (this->theBoard.size() > row)
    if (this->theBoard[row].size() > col)
        return this->theBoard[row][col];
或者在

try {
   return this->theBoard.at(row).at(col);
catch (...)
{
   std::cerr << "wrong row col size" << std::endl
}
试试看{
返回此->theBoard.at(行).at(列);
捕获(…)
{

您不需要在类成员函数中使用
这个
指针来引用类成员变量,所以

int Board::getSquare( int row, int col)
{
  vector<int> rowVector = this->theBoard[ row];
如果您确定不会访问绑定外的元素。例如,如果您不能保证这种不变,请为此添加检查

int Board::getSquare( int row, int col)
{
    if ( !( row < theBoard.size())
      throw std::out_of_range( "invalid row");

    if ( !( col < theBoard[ row].size())
      throw std::out_of_range( "invalid col");

    return theBoard[ row][ col];
}
int Board::getSquare(int行,int列)
{
如果(!(行
或者使用
std::vector::at
代替
操作符[]


使用
.at
而不是
[]
,查看是否出现异常。尽管您复制整个向量只是为了获得单个值,但此代码本身并没有任何问题。您只需
返回Board[row][col];
正如@chris指出的,您可能正在访问一个越界元素。如果您使用调试器,您就不必猜测segfault发生在哪里了。@user657267我将把它切换到您输入的return语句,实际上我在代码的另一个点上有类似的东西,我只是忘了在那一点上再做一次。而且我尝试使用调试器,但它一直说我没有调试标志,即使我在Fig.Frm文件中放置了FLAGISS=G-WALL。我不知道如何修复它。@ JoshBreece,你是指<代码> cFLAGs<代码>吗?然后,你应该使用<代码> CXXFLAGG/<代码>,除非你不依赖C++文件的隐式规则。g与
strip
-s
@JoshBreece链接上的符号仅在main.o上使用
-g
是不够的。您必须添加
$(CXXFLAGS)
添加到您构建的每个对象文件中。暂时不要使用make,使用
g++-g-O0-Wall-D_GLIBCXX\u DEBUG-DEBUG-DLOC-lm-lc main.cpp DoTheWork.cpp Board.cpp.././Utilities/Scanner.cpp.././Utilities/Scanner.cpp.././Utils.cpp-o Aprog
。或者去掉
CFLAGS
cxf滞后
,只需将
-g
添加到
GPP
(同时禁用优化)。
int Board::getSquare( int row, int col)
{
    return theBoard[row][col];
}
int Board::getSquare( int row, int col)
{
    if ( !( row < theBoard.size())
      throw std::out_of_range( "invalid row");

    if ( !( col < theBoard[ row].size())
      throw std::out_of_range( "invalid col");

    return theBoard[ row][ col];
}