?
Solved

operators and stuff

Posted on 2005-03-31
2
Medium Priority
?
290 Views
Last Modified: 2010-04-01
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
Comment
Question by:orenisraeli2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
kulpem earned 720 total points
ID: 13673544
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
 
LVL 1

Author Comment

by:orenisraeli2000
ID: 13673558
10x, I see your point
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question