Home > Mobile >  Error: Type 'Classname' does not provide a call operator
Error: Type 'Classname' does not provide a call operator

Time:04-02

Working on a program involving matrices and hit with an error involving Multiply and Transpose methods. I'm not sure how to proceed

//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;
}

Error:

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];
    ^~~

CodePudding user response:

As noted in comments, you have:

ans(i, j) = matrix_[i][j] * A;

Which is trying to call operator() on ans. But you haven't defined this operator for this type of object.

You'd either need to define that operator so this code works, or just use the existing setVal:

ans.setVal(i, j, matrix_[i][j] * A);
  •  Tags:  
  • c
  • Related