Re: Operator + overload question/error

Discussion in 'C++' started by John Harrison, Aug 16, 2003.

  1. <> wrote in message
    news:...
    > My question relates to the fact that i still have some problems
    > understanding the operator overloading. Many books say
    > the you can do this:
    >
    > ret_type operator+(arg1, arg2)
    >
    > With 2 arugments passed to it, however when I do the below in VC++ 6
    > i get errors saying too many parameters, here is my code:
    >
    > #include<iostream.h>
    >
    > class sample
    > {
    > private:
    > int i;
    > int r;
    >
    > public:
    >
    > sample()
    > { i=0; r=0}
    >
    > sample(int ii, int rr)
    > {
    > i=ii;
    > r=rr;
    > }
    >
    > sample operator+(sample s1, sample s2)
    > {
    > sample temp;
    > temp.i = s1.i +s2.i;
    > temp.r = s1.r+s2.r;
    > return temp;
    > }
    >
    > };
    >
    > void main()
    > {
    > sample a(2, 2);
    > sample b((3,3);
    >
    > sample c;
    >
    > c= a+b;
    > //should result in
    > //c.i=5
    > //c.r=5
    >
    > }
    >
    >
    > I have done this uing the single parameter form,
    > sample operator+(sample s1)
    >
    > I want to know, is what I am trying above with 2 arguments wrong,
    > because the books indicate you can do so, but compiler says too many
    > arguments, or is this a problem with specific compilers. Any advise
    > is appreciated.
    >
    > Victor
    >
    >


    The problem is that you have made the two argument version a member of your
    class. Every member function has an extra implicit argument. So a two
    argument member function really has three arguments.

    The two argument version is preferred but you must make it a global
    function, not a member function. Like this

    class sample
    {
    ...
    };

    sample operator+(const sample& s1, const sample& s2);

    You might also what to make the global function a friend of class sample.

    john
     
    John Harrison, Aug 16, 2003
    #1
    1. Advertising

  2. John Harrison

    Guest

    AWESOME explanation!.. Thanks you have really helped me understand
    so now I can move further and learn more, people who write c++ books
    should explain it in simple terms like you have and show an example,,
    appreciate your help.


    On Sat, 16 Aug 2003 23:06:38 +0100, "John Harrison"
    <> wrote:

    >
    ><> wrote in message
    >news:...
    >> My question relates to the fact that i still have some problems
    >> understanding the operator overloading. Many books say
    >> the you can do this:
    >>
    >> ret_type operator+(arg1, arg2)
    >>
    >> With 2 arugments passed to it, however when I do the below in VC++ 6
    >> i get errors saying too many parameters, here is my code:
    >>
    >> #include<iostream.h>
    >>
    >> class sample
    >> {
    >> private:
    >> int i;
    >> int r;
    >>
    >> public:
    >>
    >> sample()
    >> { i=0; r=0}
    >>
    >> sample(int ii, int rr)
    >> {
    >> i=ii;
    >> r=rr;
    >> }
    >>
    >> sample operator+(sample s1, sample s2)
    >> {
    >> sample temp;
    >> temp.i = s1.i +s2.i;
    >> temp.r = s1.r+s2.r;
    >> return temp;
    >> }
    >>
    >> };
    >>
    >> void main()
    >> {
    >> sample a(2, 2);
    >> sample b((3,3);
    >>
    >> sample c;
    >>
    >> c= a+b;
    >> //should result in
    >> //c.i=5
    >> //c.r=5
    >>
    >> }
    >>
    >>
    >> I have done this uing the single parameter form,
    >> sample operator+(sample s1)
    >>
    >> I want to know, is what I am trying above with 2 arguments wrong,
    >> because the books indicate you can do so, but compiler says too many
    >> arguments, or is this a problem with specific compilers. Any advise
    >> is appreciated.
    >>
    >> Victor
    >>
    >>

    >
    >The problem is that you have made the two argument version a member of your
    >class. Every member function has an extra implicit argument. So a two
    >argument member function really has three arguments.
    >
    >The two argument version is preferred but you must make it a global
    >function, not a member function. Like this
    >
    >class sample
    >{
    > ...
    >};
    >
    >sample operator+(const sample& s1, const sample& s2);
    >
    >You might also what to make the global function a friend of class sample.
    >
    >john
    >
     
    , Aug 17, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  2. Gianni Mariani
    Replies:
    5
    Views:
    1,003
    Jerry Coffin
    Jan 18, 2005
  3. Sean
    Replies:
    7
    Views:
    408
    Kai-Uwe Bux
    Oct 6, 2005
  4. sd2004

    Overload operator error ?

    sd2004, Jan 19, 2006, in forum: C++
    Replies:
    1
    Views:
    361
    Mike Wahler
    Jan 19, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page