overloading the + operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meLlamanJefe
    New Member
    • Mar 2008
    • 29

    overloading the + operator

    Hello,

    I have a short matrix class I am trying to set up for a small project. I would like to overload the + operator to simplify the code syntax. However I am running into a compilation issue that I am not familiar with. Here is a snippet of the relevant code:

    Code:
    // File Matrix.h
    
    class Matrix
        {
        public:
            Matrix(int Rows);
            Matrix(int Rows, int Columns);
            ~Matrix();
    
             double &operator() (int Row, int Column)
            {
               if(Row < 0 || Column < 0 || Row > mRows || Column > mColumns)
               {
                  // Out of range error
               }
               
               return(mData[(mColumns * Row) + Column]);
            }
    
            Matrix operator + (Matrix &B)
            {
               Matrix *A = this;
               if((A->GetRowCount() != B.GetRowCount()) ||
                  (A->GetColCount() != B.GetColCount()))
               {
                  // Matrices do not have the same dimensions
                  return(*A);
               }
    
               Matrix C(A->GetRowCount(), A->GetColCount());
               for(int i = 0; i < A->GetRowCount(); ++i)
               {
                  for(int j = 0; j < A->GetColCount(); ++j)
                  {
                     C(i, j) = A(i, j) + B(i, j); // This is line 78 as referenced by the error below
                  }
               }
               return(C);
            }
    
         private:
            int mRows;
            int mColumns;
            double *mData;
       }
    And here is the error I am receiving from the compiler:

    Matrix.h: In member function `FlightSim::Mat rixFlightSim::M atrix::operator +(FlightSim::Ma trix&)':
    Matrix.h:78: `A' cannot be used as a function
    make: *** [Matrix.o] Error 1

    I should mention that I am fairly new at C++ so I recognize my inexperience is sure to be a factor here.

    Thanks!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    A is a pointer to a Matrix, B is a Matrix, so it should be evident you need to treat them differently. You have been in the rest of the function, but here you write both A(i, j) and B(i, j). You may have to use A->(i, j) or some similar syntax, I'm not sure which.

    Comment

    • scruggsy
      New Member
      • Mar 2007
      • 147

      #3
      Originally posted by Ganon11
      A is a pointer to a Matrix, B is a Matrix, so it should be evident you need to treat them differently. You have been in the rest of the function, but here you write both A(i, j) and B(i, j). You may have to use A->(i, j) or some similar syntax, I'm not sure which.
      You need to dereference A to get at the object it points to:
      Code:
      (*A)(i, j)
      Your compiler will probably reject A->(i, j).
      If you want to make your code more readable, I suggest:
      Code:
      Matrix A = *this;

      Comment

      • meLlamanJefe
        New Member
        • Mar 2008
        • 29

        #4
        Originally posted by scruggsy
        You need to dereference A to get at the object it points to:
        Code:
        (*A)(i, j)
        Your compiler will probably reject A->(i, j).
        If you want to make your code more readable, I suggest:
        Code:
        Matrix A = *this;
        Thanks scruggsy. I certainly had tried A->(i, j) and as you mentioned the compiler did not like it either. Anyhow, I like the suggestion to dereference "this" so that's what I tried and it works like a charm.

        Comment

        Working...