Search code examples
c++assignment-operator

C++ no match for 'operator='


I'm currently working on an assignement concerning mobile robots. I'm developing on Windows with QT-Creator, using CMake and Visual C++ Compiler 10.0.

As the robot's working on Ubuntu, I need to compile the projekt using QT-Creator on Ubuntu with the GNU x86 compiler.

Now here's the problem, concerning the class Box, which is just storing some int-values:

Box.h

public:
Box();                                                                  
Box(int name, int sX, int sY, int width, int heigth);               
Box& operator = (Box &src);                                             
bool operator == (Box &src);                                     

private:
int name;                                                              
int startX, startY, endX, endY;                                       
int cgx, cgy;                                                        
int width, height;

There are also some get-methods in this header (like int getName() ).

The overloaded assignment-operator looks as following:

Box &Box::operator =(Box &src){
    this->cgx = src.getCenterX();
    this->cgy = src.getCenterY();
    this->endX = src.getEndX();
    this->endY = src.getEndY();
    this->startX = src.getStartX();
    this->startY = src.getStartY();

   return *this;} 

I'm using Box objects in another class, stored in a std::list<Box>.

On Windows, everything works fine. But on Ubuntu I get the error-message no match for 'operator=' (operand types are 'Box' and 'const Box') , which occurs in a function of std::list.

I'm quite new to C++ and haven't used const explicit anywhere. So how does this error happen and how do I solve it?


Solution

  • If you want to manually implement the assignment operator=() for your Box class (note that the compiler will implement a default member-wise assignment operator= if you don't provide one), consider following a pattern something like this:

    1. use const Box& (not just Box&) as the input source type, since you are not modifying the source;
    2. check for self-assignment inside the overloaded operator implementation (if (&src != this)...).

    In code:

    Box& Box::operator=(const Box& src) 
    {
        if (&src != this) 
        {
            this->cgx = src.getCenterX();
            this->cgy = src.getCenterY();
            this->endX = src.getEndX();
            this->endY = src.getEndY();
            this->startX = src.getStartX();
            this->startY = src.getStartY();
        }
        return *this;
    } 
    

    Note also that if you manually define a copy assignment operator=(), you may also want to define a copy constructor, probably following the same copy logic implemented in your assignment operator=():

    Box(const Box& src) :
        cgx   ( src.getCenterX() ),
        cgy   ( src.getCenterY() ),
        endX  ( src.getEndX()    ),
        endY  ( src.getEndY()    ),
        startX( src.getStartX()  ),
        startY( src.getStartY()  )
    {}
    

    NOTE

    You have a data member name in your Box class that you are not copying. Is this a typo? Are you really sure you don't want to copy it? Maybe this is a bug?