• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

operators and stuff

hi there,
I have a matrix class, I need to build an operator that will do
int main(){
   matrix a,b;
   .
   .
   .
   a = b + 2;
   .
   .
   return 0;
}
this one I got. I did it this way
matrix matrix::operator +(int x){
      matrix sum(iRows,iColumns);
      for(int i=0;i<getRows();i++){
         for(int j=0;j<getColumns();j++){
               sum.mat[i][j] = mat[i][j] + x;
            }
      }
      return sum;      
}

now I need an operator like this
int main(){
   matrix a,b;
   .
   .
   .
   a = 2 + b;
   .
   .
   return 0;
}

where the '2' is on the left side of the '+' sign how do I do it????
I allready have an operator for '+', one as described above and another for adding to class memebrs. like this
matrix matrix::operator +(matrix &m){
      if(!((this->getColumns() == m.getColumns()) && (this->getRows() == m.getRows()))){
            return *this;
      }
      matrix sum(m.iRows,m.iColumns);
      for(int i=0;i<m.getRows();i++){
         for(int j=0;j<m.getColumns();j++){
               sum.mat[i][j] = mat[i][j] + m.mat[i][j];
            }
      }
      return sum;      
}
so how do I make the '2' add to the matrix???
10x
just the prototype will do!!!
0
orenisraeli2000
Asked:
orenisraeli2000
1 Solution
 
kulpemCommented:
you'll need to do it outside the class
cause compiler is looking for 2.operator(matrix);
u dont have it, so try,,
matrix operator+(int x,matrix &m){};
outside the class. ofcourse you'll need to use set & get function to to access data
goodluck
0
 
orenisraeli2000Author Commented:
10x, I see your point
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now