Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post

    Exclamation ERROR in program.... i cant understand

    This my program which works:
    #include <iostream.h>
    class circle
    {
    private:
    int r;
    int x,y;
    public:
    circle()
    {
    x=y=r=0;
    cout<<"\nNew object created";
    showData();
    }
    circle(int xx,int yy,int rr)
    {
    x=xx;y=yy;r=rr;
    cout<<"\nNew object created";
    showData();
    }
    void showData()
    {
    cout<<"\n(x.y)="<<x<<","<<y<<" r="<<r;
    }


    circle& operator=(circle& a)
    {
    x=a.x;
    y=a.y;
    r=a.r;
    return *this;
    }
    };

    void main()
    {
    circle x(10,5,5);
    circle y(10,10,5);
    circle z;
    z=x;
    x=y;
    y=z;
    x.showData();
    y.showData();
    }


    NOW.. if i change in the main() function circle z; to
    circle z();
    then the following error occurs:
    Compiling...
    circle_thispointer2.cpp

    error C2659: '=' : overloaded function as left operand

    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class circle (__cdecl *)(void)' (or there is no acceptable conversion)
    P.S: THIS IS NOT A HOME WORK..lol
    Error executing cl.exe.

    Can someone pls explain why it occurs? and its mechanism
    Last edited by adarshakb; 07-04-2009 at 06:34 AM. Reason: people misinterpret this as homework... but at my class they havent even touched classes!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by adarshakb View Post
    NOW.. if i change in the main() function circle z; to
    circle z();
    then the following error occurs:
    Compiling...
    circle_thispointer2.cpp

    error C2659: '=' : overloaded function as left operand

    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class circle (__cdecl *)(void)' (or there is no acceptable conversion)
    Error executing cl.exe.

    Can someone pls explain why it occurs? and its mechanism
    z() is function, z is variable. is a syntax problem.

    best regards

  • Users who have thanked oesxyl for this post:

    adarshakb (07-04-2009)

  • #3
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    a variable and function diference.... and i didnt know that.. thks..
    another question

    Then why does the compiler give error about the overloaded operator??

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by adarshakb View Post
    a variable and function diference.... and i didnt know that.. thks..
    another question

    Then why does the compiler give error about the overloaded operator??
    compiler try to guess what was wrong and jump to conclusion that you intend to overload the () operator but it fail to find the declaration,

    best regards

  • Users who have thanked oesxyl for this post:

    adarshakb (07-04-2009)

  • #5
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    Quote Originally Posted by oesxyl View Post
    compiler try to guess what was wrong and jump to conclusion that you intend to overload the () operator but it fail to find the declaration,

    best regards

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by adarshakb View Post


    is oftopic but I would prefere to implement circle constructor like this:
    Code:
    circle::circle(int ax = 0, int ay = 0, int ar = 0){
      x= ax; y=ay; r=ar;
      // other things...
    }
    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •