1

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?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • 1
    `Box& operator = (const Box &src);` – Borgleader Jan 13 '15 at 15:15
  • in addition to the other comments, `bool operator == (const Box &src) const`; for `==` as well (`const &` parameter and mark the method as `const`). – crashmstr Jan 13 '15 at 15:17
  • ... and then marking all of the `get...` methods const, as well, so they can be called on `src`. These are generally good practices, anyway, not just solutions to this specific problem. – Paul Roub Jan 13 '15 at 15:18
  • 1
    @sharptooth completely different set of operators, this is the assignment operator. Not the equality compare. – Mgetz Jan 13 '15 at 15:20
  • I would suggest a reading of http://stackoverflow.com/questions/4421706/operator-overloading – vsoftco Jan 13 '15 at 15:20
  • 1
    I would say operator = not copying the name is counter-intuitive. – Neil Kirk Jan 13 '15 at 15:25
  • You are very likely using the assignment operator with an rvalue which can only bind to `const` lvalue reference in C++98. – Mustafa Jan 13 '15 at 15:25
  • @Mgetz: Yeap. That was a typo. I was talking about implementing `operator!=()` by reusing `operator==()`. – sharptooth Jan 13 '15 at 15:27

3 Answers3

2

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?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
1

An assignment operator should accept a const object:

Box& operator = (const Box& src);

Because it will not affect src but only this.

When you define the copy operator, you should also define the copy constructor. See http://en.cppreference.com/w/cpp/language/rule_of_three

An comparison operator should accept a const object and should be const (the function does not change this).

bool operator == (const Box& src) const;

Because it will not affect src, nor this.

Alessandro Pezzato
  • 8,603
  • 5
  • 45
  • 63
1

Try defining the parameter as const:

Box& operator = (const Box &src);                                             

Also, you should do the same for the comparison, even though it's not the source of your problem:

bool operator == (const Box &src) const;  

I suspect the right hand side of your assignment or comparison is const. Note that this patern is good practice as you are not modifying your parameters.

I've also made the comparison const as you won't be modifying this.

Sean
  • 60,939
  • 11
  • 97
  • 136