我正在嘗試使用標準庫向量爲Matrix創建類。我使用矢量內的矢量來設置矩陣,一個矢量表示列,另一個(矢量)表示存儲在行中的行和值。這裏是變量和構造函數。用向量嵌套在向量中轉儲的分段錯誤核心
變量:
int columns;
int rows;
std::vector<std::vector<int> > v;
構造:
Matrix(int a, int b){
std::cout << "Input Recieved.. Construct Began" << std::endl;
rows = a;
columns = b;
// Subtract one to put them in a proper array format
rows = rows - 1;
columns = columns - 1;
//Creates the columns
v.reserve(columns);
//Creates the Rows .. Code is ran for every column, this is where the values are set
for(int i = 0; i <= columns; i++){
v[i].reserve(rows);
std::cout << "Column " << i + 1 << " Created, with " << rows + 1<< " Rows" << std::endl;
//Sets the values of the rows .. is ran for every column
for(int e = 0; e <= rows; e++){
if(i == 19){
std::cout << "Column 20 row setting has begun" << std::endl;
}
v[i][e] = 2;
if(i == 19){
std::cout << "Made it past the line" << std::endl;
}
std::cout << "Row " << e + 1 << " Set in Column " << i + 1<< ", with Value " << v[i][e] << std::endl;
if(i == 19){
std::cout << "Column 20 row setting has finished" << std::endl;
}
}
}
}
現在,它似乎能夠除了最後一個載體創造的一切,然後我得到段錯誤。對於一個更完整的源代碼代碼有這個http://pastebin.com/AB59bPMR。
那麼,你是否在調試器中遍歷代碼並查看錯誤的位置? – OldProgrammer
@OldProgrammer分段錯誤(核心轉儲)是我所得到的。 – Noah
這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver