我正在尝试使用标准库向量为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