error in operator overloading

Discussion in 'C++' started by alirooni, May 5, 2010.

  1. alirooni

    alirooni New Member

    Joined:
    May 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi
    i have a problem in this code:
    Code:
    #include<iostream.h>
    class exforsys{
    private:
    int x,y;
    public:
    exforsys(int x,int y){x=0; y=0;}
    void getvalue(){
    cout<<"\n enter value for x:";
    cin>>x;
    cout<<"\n enter value for y:";
    cin>>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:";
    e1.getvalue();
    cout<<"enter value for obgect e2:";
    e2.getvalue();
    e3=e2+e1;
    }
    "could not find a match for exforsys::exforsys()" compiler says for the line after where the main function starts.what is my fault!?
     
    Last edited by a moderator: May 6, 2010
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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);"
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice