Error from a matrix class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rijaalu
    New Member
    • Jan 2010
    • 2

    Error from a matrix class

    I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error.

    Code:
    include <iostream>
    using namespace std;
    
    class matrix{
         
    public:
          
    matrix();
    matrix(int m,int n);
    
    int getRow();
    int getCol();
    
    double& operator()(int, int);
    
    friend ostream& operator<<(ostream& os, matrix& m);
    matrix operator + (matrix&);
    matrix operator * (matrix&);
    matrix operator - (matrix&);
    matrix operator / (matrix&);
    
    private:
    void init(int, int);
    int nrows,ncols;
    double *data;
    };
    
    matrix::matrix(){
    init(1,1);
    }
    
    matrix::matrix(int m, int n){
    init(m,n);
    }
    
    void matrix::init(int m, int n){
    nrows=m;
    ncols=n;
    data= new double[m*n];
    for(int i=0; i<m*n; i++)
    data[i]=0;
    }
    
    int matrix::getRow() { return nrows;}
    
    int matrix::getCol() { return ncols;}
    
    double& matrix::operator ()(int r, int c){
    if (r <0 || r> nrows){
    cout<<"Illegal row index";
    return data[0];
    }
    else if (c <0 || c > ncols){
    cout<<"Illegal Column Index:";
    return data[0];
    }
    else return data[r*ncols+c];
    }
    
    ostream& operator<<(ostream& os, matrix &m){
    int mval=m.getRow();
    int nval=m.getCol();
    for(int i=0; i<mval; i++){
    for(int j=0; j < nval; j++)
    os<<m(i,j);
    os<<endl;
    }
    return os;
    }
    
    matrix matrix::operator+(matrix& a){
    matrix sum(nrows, ncols);
    for (int i=0; i<nrows; i++)
    for(int j=0; j<ncols; j++)
    sum(i,j) = (i,j) + a(i,j);
    return sum;
    
    matrix matrix::operator*(matrix& a){
    matrix product(nrows, ncols);
    for (int i=0; i<nrows; i++)
    for(int j=0; j<ncols; j++)
    product(i,j) = (i,j) * a(i,j);
    return product;
    
    matrix matrix::operator-(matrix& a){
    matrix difference(nrows, ncols);
    for (int i=0; i<nrows; i++)
    for(int j=0; j<ncols; j++)
    difference(i,j) = (i,j) - a(i,j);
    return difference;
    
    
    matrix matrix::operator/(matrix& a){
    matrix divide(nrows, ncols);
    for (int i=0; i<nrows; i++)
    for(int j=0; j<ncols; j++)
    divide(i,j) = (i,j) / a(i,j);
    return divide;
    
    
    }
    int main(){
    matrix a(2,2);
    a(1,1)=5.0;
    a(0,0)=6.0;
    a(0,1)=7.0;
    a(1,0)=8.0;
    cout<<a(0,0)<<endl;
    cout<<a(1,1)<<endl;
    cout<<a<<endl;
    
    matrix b(2,2);
    b(0,0) = 5.0;
    b(0,1) = 5.0;
    b(1,0) = 5.0;
    b(1,1) = 5.0;
    cout<<b<<endl;
    
    matrix c,d,e,f;
    
    c=a+b;
    d=a*b;
    e=a-b;
    f=a/b;
    
    cout<<c<<endl;
    cout <<"\n\n";
    cout<<d<<endl;
    
    cout<<e<<endl;
    cout <<"\n\n";
    cout<<f<<endl;
    
    
    system("pause");
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    double& operator()(int, int);
    This won't work.

    The rules of operator overloading require that ast least one argument be a user-defined type. Like a class or a struct.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      It's a member of the matrix class and compiles without errors. The reason of the error is missing '#' in #include directive and missing closing brace in operator+ implementation (and other operators as well)

      Comment

      Working...