2016-11-29 11 views
2

私は、コンストラクタ内のオブジェクトのクラスのメンバであるベクトルを生成しようとしています。私は実行時に境界外のエラーのベクトルインデックスを受け取りました。デバッガを使用して実行中にこのエラーが発生した理由を確認するために、プログラムに入りました。明らかに、たとえ私がコンストラクタでそうしたとしても、ベクトルは決して初期化されませんでした。私は下に私のコードスニペットを持っている、私はそれをより理解できるようにコメントを追加しました。どんな助けでも大歓迎です。ここ コンストラクタがベクターを初期化しないのはなぜですか?

エラーが

発生源の.cppの一部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"))); 
} 

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; 

}; 

さであります

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 

      } 
     } 
    } 
+2

メンバー変数を隠すコンストラクタで再宣言しました。あなたが他のメンバ変数を正しく初期化したために奇妙なのはなぜですか?maze_graphの型を宣言に変換する必要性を感じたのはなぜですか? – Borgleader

+2

'for(f = 0; f PaulMcKenzie

+0

woops、私は@PaulMcKenzieありがとう! – Flower

答えて

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アイテムを内部的に計算するインデックス機能を提供するだけです。

関連する問題