我正在研究一个由二维双精度向量组成的 C++ 类。我即将创建 2D 矢量,但是当我尝试编辑其中的值时,程序崩溃了。我尝试使用 [][] 运算符并将其设置为等于 myDub 并且我尝试使用类似 myMat.editSlot(i,j,myDub) 的类,并且两者都导致程序崩溃。
//n == # of rows and cols (所有矩阵都是正方形) //infile 正确打开文件
mat my_mat(n,n);
// Read input data
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
double myDub;
inFile >> myDub;
my_mat.editSlot(i,j,myDub);
}
}
这是课程:
class mat
{
mat( int x , int y ) {
int row = x;
int col = y;
vector<vector<double>> A( row , vector<double>( row , 0 ) );
for ( int i = 0; i<row; i++ )
{
for ( int j = 0; j<col; j++ )
{
cout << setw( 6 ) << A[i][j];
}
cout << endl;
}
}
void editSlot( int x , int y , double val ) {
A[x][y] = val;
}
vector<vector<double>> A;
private:
int n;
};