problem with operators = and multiple +

Discussion in 'C++' started by david, Feb 19, 2008.

  1. david

    david Guest

    The code is located here:
    http://www.paste.lt/paste/fafbd9fbfc71e25f12458aacf61aca8f

    Before telling the problem I am now having and I would like to you
    that there is still a lot of mistakes, missing parts, "const" words
    and etc, but it works as it should be. I will make it better after I
    will solve my problem.


    So I have problem with = and + and even * (just wrote this part of
    code) and I think I will be having in the future if I won't handle and
    understand it now. The problem is in 352 line (there are two lines).
    If I try to do like this: aaa + bbb + ccc it does work and returns new
    intance of Aibe (not pointer or reference), aaa = bbb works just fine
    too, it should make a deep copy of bbb (there still should be some
    correction in code, like checking a = a, freeing memory of the first
    one and etc.), but the code aaa = bbb + ccc + ... just does not work.
    I am using gcc under Linux/Solaris/Mac OS X and I am getting this
    error:

    Macbook:pirma marius$ g++ -Wall -ansi -pedantic -o pirma pirma.cpp
    pirma.cpp: In function 'int main(int, const char**)':
    pirma.cpp:358: error: no match for 'operator=' in 'aaa =
    Aibe::eek:perator*(Aibe&)(((Aibe&)(& ccc)))'
    pirma.cpp:238: note: candidates are: Aibe& Aibe::eek:perator=(Aibe&)
    pirma.cpp:359: error: no match for 'operator=' in 'aaa = Aibe::eek:perator
    +(Aibe&)(((Aibe&)(& ddd)))'
    pirma.cpp:238: note: candidates are: Aibe& Aibe::eek:perator=(Aibe&)

    Could someone help with this?
    It looks that I made some mistakes declarating operators or there is
    some problems with returning types.

    P.S. Sorry for making mistakes, too tired to make something correct.
    david, Feb 19, 2008
    #1
    1. Advertising

  2. david schrieb:
    > The code is located here:
    > http://www.paste.lt/paste/fafbd9fbfc71e25f12458aacf61aca8f
    >
    > Before telling the problem I am now having and I would like to you
    > that there is still a lot of mistakes, missing parts, "const" words
    > and etc, but it works as it should be. I will make it better after I
    > will solve my problem.
    >
    >
    > So I have problem with = and + and even * (just wrote this part of
    > code) and I think I will be having in the future if I won't handle and
    > understand it now. The problem is in 352 line (there are two lines).
    > If I try to do like this: aaa + bbb + ccc it does work and returns new
    > intance of Aibe (not pointer or reference), aaa = bbb works just fine
    > too, it should make a deep copy of bbb (there still should be some
    > correction in code, like checking a = a, freeing memory of the first
    > one and etc.), but the code aaa = bbb + ccc + ... just does not work.
    > I am using gcc under Linux/Solaris/Mac OS X and I am getting this
    > error:


    The problem is the missing "const" words and etc. :)

    For example, your operator+ here:
    Aibe operator + (Aibe &two);
    should be:
    Aibe operator + (const Aibe& two) const;

    When you write
    a = b + c;
    the operator+ member function returns a temporary object. Temporaries
    cannot be bound to non-const references, so all your functions taking only
    non-const references won't take the result of calling one of your binary
    operators.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    post tenebras lux. post fenestras tux.
    Thomas J. Gritzan, Feb 19, 2008
    #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. Przemek
    Replies:
    4
    Views:
    279
    Rolf Magnus
    Jun 22, 2004
  2. mathieu

    Multiple suitable operators

    mathieu, Jan 19, 2010, in forum: C++
    Replies:
    9
    Views:
    285
    James Kanze
    Jan 21, 2010
  3. Philip Müller
    Replies:
    4
    Views:
    99
    Robert Klemme
    Apr 18, 2009
  4. Iain Barnett
    Replies:
    0
    Views:
    83
    Iain Barnett
    Feb 2, 2011
  5. Replies:
    0
    Views:
    80
Loading...

Share This Page