dym*_*ymk 4 c++ class vector nested-class
我有嵌套向量中向量问题,二维数组在C相当于我已经试过代码演示这张贴在众多的网站,都无济于事.
class Board
{
    public:
        vector< vector<Cell> > boardVect; //Decalre the parent vector as a memebr of the Board class    
        Board()
        {
            boardVect(3, vector<Cell>(3)); //Populate the vector with 3 sets of cell vectors, which contain 3 cells each, effectivly creating a 3x3 grid. 
        }
};
当我尝试编译时,我收到此错误:
F:\ main.cpp | 52 |错误:无法匹配调用'(std :: vector>)(int,std :: vector)'
第52行是: boardVect(3, vector<Cell>(3));
在使用3个向量类构造父向量时,是否出现错误?
Ben*_*ley 12
您需要使用初始化列表才能在类的成员上调用构造函数,即:
Board()
    :boardVect(3, vector<Cell>(3))
{}
一旦你进入了构造函数的主体,就太晚了,所有的成员都已经构造好了,你只能调用非构造函数的成员函数.你当然可以这样做:
Board()
{
    boardVect = vector<vector<Cell> >(3, vector<Cell>(3));
}
但初始化列表是首选.