为什么构造函数不初始化向量?

时间:2016-11-29 20:14:24

标签: c++ vector constructor

我正在尝试填充一个向量,该向量是构造函数中对象类的成员。我在运行时收到了一个向量索引超出范围的错误。然后我在使用调试器运行时进入程序,看看为什么会出现这个错误。显然,即使我在构造函数中这样做,向量也从未被初始化。我在下面有我的代码片段,我添加了评论以使其更容易理解。非常感谢任何帮助。

这是maze.h代码段

class maze
{
public:

int numlevels, numrows, numcols;
int startlevel, startrow, startcol, endlevel, endrow, endcol;
int level, row, col;

// here is the construtor declaration
maze(int l, int m, int n, int o, int p, int q, int r, int s, int t);

~maze();


//these two lines can be ignored
void solve(int startlevel, int startrow, int startcol, int endlevel, int    endrow, int endcol);
queue<char> q;

//I declare the 3d array here as a public member
vector<vector<vector<string> > > mazeGraph;

};

这是maze.cpp代码段

//here is the constructor body
maze::maze(int l, int m, int n, int o, int p, int q, int r, int s, int t)
{
numlevels = l;
numrows = m;
numcols = n;
startlevel = 0;
startrow = p;
startcol = q;
endlevel = r;
endrow = s;
endcol = t;
//ignore the redundancy of these assignments for now.

//i try to initialize the array here
vector<vector<vector<string> > > mazeGraph(numlevels, vector<vector<string>  >(numrows, vector<string>(numcols, "0")));
}

这是发生错误的源.cpp的一部分

for (f = 0; f < numMazes; i++)
{
    //these variables are read from a text file and are used to initialize an object's vector member.
    //nothing is wrong with the file reading, these variables are updated correctly

    infile >> numlevels >> numrows >> numcols;
    infile >> startlevel >> startrow >> startcol;
    infile >> endlevel >> endrow >> endcol;

    //mazeList is an array of maze objects. each maze object has its own 3 dimensional array which i am trying to initialize with the above variables.
    mazeList.push_back(maze(numlevels,numrows,numcols,startlevel,startrow,startcol,endlevel,endrow,endcol));






     //mazeList.at(f).numlevels refers to the numlevels member belonging to the fth maze object in the list of mazes.
    //this is the same for numrows and numcols
    //these member variables are updated corretly
    for (i = 0; i < mazeList.at(f).numlevels; i++)
    {
        for (j = 0; j < mazeList.at(f).numrows; j++)
        {
            for (k = 0; k < mazeList.at(f).numcols; k++)
            {
                //the text file contains strings that are to be put into a 3-d array
                //mazeGraph is the 3-d vector belonging to the current maze object
                //mazeList.at(f).mazeGraph refers to the mazeGraph belonging to the current maze in the list.
                infile >> mazeList.at(f).mazeGraph[i][j][k];
                //the above line generates the error

            }
        }
    }

1 个答案:

答案 0 :(得分:1)

本地声明

vector<vector<vector<string> > > mazeGraph(numlevels, vector<vector<string>  >(numrows, vector<string>(numcols, "0")))

在构造函数中,只声明一个名为mazeGraph的局部向量。这与名为mazeGraph的数据成员无关。

要初始化成员,您可以使用构造函数初始值设定项列表,也可以分配给它。

具有相同问题的更简单情况的示例:

struct S
{
    int x;
    S() { int x = 666; }   //! Ungood, just a local declaration!
};

struct T
{
    int x;
    T(): x{42} {}   // OK, initializes data member `x`.
};

struct U
{
    int x;
    U(){ x = 42; }    // Also OK, not ideal but initializes member `x`.
};

为了提高效率和简单性,根据提供存储的单个底层向量来实现3D阵列是个好主意。只需提供一个索引函数,在内部计算给定3D索引要访问的1D项目。