Operator overloading problem

This is a discussion on Operator overloading problem within the C++ Programming forums, part of the General Programming Boards category; Im having trouble with the operator +. I'm trying to overload it so it can be used with one of ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Operator overloading problem

    Im having trouble with the operator +. I'm trying to overload it so it can be used with one of my classes but it doesnt seem to work when i do this:
    Code:
    var1= var2+ var3;
    I keep getting this error:

    C:\Code\Projects\MyLib\main.cpp:9: error: no match for 'operator=' in 'result = (&string1)->CBuffer:perator+(((CBuffer&)(&string2)))


    Heres my definitions for the overloads:
    Code:
    CBuffer& operator=(CBuffer& pBuffer);
    CBuffer operator+(CBuffer& pOther);
    The only solution i could think of was changing the + overload to:

    Code:
    CBuffer& operator+(CBuffer& pOther);
    But that required a static return object...not sure if thats the best way around it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You should change it to be a free function:
    Code:
    CBuffer operator+(const CBuffer& lhs, const CBuffer& rhs);
    If necessary, make it a friend function, though I recommend implementing it in terms of an operator+= such as:
    Code:
    CBuffer& CBuffer::operator+=(const CBuffer& rhs)
    Incidentally, your copy assignment operator should take a const CBuffer&, not a CBuffer&, as its argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I tried to define outside the class like you have, but get the same error.
    Anyways i made it like this:

    Code:
    class CBuffer
    {
      static CBuffer retBuffer;
      ..
      ..
    };
    and in all my sub functions where i need to return a new CBuffer i do this:

    Code:
    CBuffer& CBuffer::operator+(CBuffer& pOther)
    {
      retBuffer.clear();
      ...
      return retBuffer;
    }
    Btw how does putting const affect it?

    Im using mingw btw
    Last edited by 39ster; 04-24-2007 at 06:24 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I tried to define outside the class like you have, but get the same error.
    I am not entirely sure what is the problem, so I am guessing that it is const-correctness.

    Btw how does putting const affect it?
    You are not going to (and should not) change the arguments passed. In any case, your copy assignment operator is wrong, and needs to be const-correct.

    I think you should remove your static CBuffer member variable (is that even legal?), and are you sure you need an operator+? Do not overload operators unless it is necessary and intuitive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21