處理涉及矩陣的程式并遇到涉及Multiply和Transpose方法的錯誤。我不確定如何進行
//Header file
class Matrix
{
public:
Matrix(); // constructor
void initIdentity(int n); // initialize as an identity matrix of size nxn
void initFromFile(string fileName); // read the matrix from a file
bool isSquare(); // test whether matrix is square
int numRows(); // return number of rows
int numCols(); // return number of columns
double getVal(int row, int col); // return matrix value at given row/col location (0-indexed based)
void setVal(int row, int col, double val); // set the matrix value at given row/col location (0-index based)
Matrix Multiply(Matrix B); // post-multiply by B and return resulting matrix
Matrix Multiply(double A); // multiply by a scalar and return resulting matrix
Matrix Transpose(); // return transpose of matrix
vector<double> Diagonal(); // return a vector containing diagonal elements of the matrix
void Print(); // print the matrix to stdout
void Print(string name); // print the matrix to stdout with a name prefix
Matrix(int row, int col, double val); // initializing a matrix
private:
vector< vector<double> > matrix_;
vector< vector<int> > identMatrix_;
vector<double> innerVec_;
int numRows_;
int numCols_;
};
//Main file
Matrix Matrix::Multiply(double A){
Matrix ans(numRows_, numCols_, 0.0);
for (int i=0; i< numRows_; i ){
for (int j=0; j< numCols_; j ){
ans(i, j) = matrix_[i][j] * A;
}
}
return ans;
}
Matrix Matrix::Transpose(){
Matrix ans(numRows_, numCols_, 0.0);
for (int i=0; i<numRows_; i ){
for (int j=0; j<numCols_; j ){
ans(i, j) = matrix_[j][i];
}
}
return ans;
}
錯誤:
Main.cpp:110:7: error: type 'Matrix' does not provide a call operator
ans(i, j) = matrix_[i][j] * A;
^~~
Main.cpp:120:5: error: type 'Matrix' does not provide a call operator
ans(i, j) = matrix_[j][i];
^~~
uj5u.com熱心網友回復:
如評論中所述,您有:
ans(i, j) = matrix_[i][j] * A;
這是試圖operator()呼吁ans。但是您還沒有為這種型別的物件定義此運算子。
您要么需要定義該運算子以便此代碼有效,要么只使用現有的setVal:
ans.setVal(i, j, matrix_[i][j] * A);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455274.html
標籤:C
