Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   error in operator overloading (http://www.go4expert.com/forums/error-operator-overloading-t22014/)

alirooni 6May2010 01:30

error in operator overloading
i have a problem in this code:

class exforsys{
int x,y;
exforsys(int x,int y){x=0; y=0;}
void getvalue(){
cout<<"\n enter value for x:";
cout<<"\n enter value for y:";
void displayvalue(){
cout<<"value of x is:"<<"value of y is:"<<y; }
exforsys operator + (exforsys);
exforsys exforsys :: operator +(exforsys e2){
int x1=x+e2.x;
int y1=y+e2.y;
return exforsys(x1,y1);
void main(){
exforsys e1,e2,e3;
cout<<"enter value for object e1:";
cout<<"enter value for obgect e2:";

"could not find a match for exforsys::exforsys()" compiler says for the line after where the main function starts.what is my fault!?

xpi0t0s 6May2010 13:12

Re: error in operator overloading
You have only defined one constructor: exforsys(int x,int y) which takes two arguments.

So you cannot use the syntax "exforsys e1;" to create an object, because this requires the constructor exforsys() -- no arguments -- to be defined.

xpi0t0s 6May2010 13:16

Re: error in operator overloading
Also there is another fault. "exforsys(int x,int y){x=0; y=0;}" will overwrite the x and y values passed in with zero. It will _not_ update the private x and y.

One possible solution is to give the parameters default values: "exforsys(int _x=0,int _y=0){x=_x; y=_y;}". Then "exforsys e1;" will be equivalent to "exforsys e1(0,0);"

All times are GMT +5.5. The time now is 04:25.