no match for 'operator==' even though the operator is defined.

Hi guys.

I'm having an annoying issue with my code. I created class named box. In that class there is a vector of another class, lipids. For that class, I overloaded the == operator by comparing the two vectors. Even though I got no error message on the operator== declaration, I still get error message when I try to use it. I have attached my box.cpp code. The bolded line is the line that cause the compilation error.

Thanks, Yotam.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "box.h"
#include "lipid.h"
#include "ball.h"

#include <iostream>

using namespace std;
box::box() {
	// TODO Auto-generated constructor stub

}

box::~box() {
	// TODO Auto-generated destructor stub
}

bool box::operator== (box other) {
	return (lipids == other.getLipids());
}

void box::calcU(box other){
	if (this==other) cout <<"other";
}
"this" is a pointer, so it wants to call this function:

 
bool operator==( box* lhs, box rhs );


You want

 
if( *this == other ) ...

You should probably use

1
2
3
bool box::operator== (const box & other) {
	return (lipids == other.getLipids());
}


which will also require that getLipids be declared 'const', but prevent the instantiation of a new 'box' object every time this comparison is done.

--Rollie
Also operator==() itself should be const, and calcU() should probably take its parameter by const reference, etc
etc.
On that point, I tried to understand the logic behind the cons usage in the operator overload and I coulsdn't see any. As far as I understand, const means that I cannot change the value inside the argument, am I right?

Thanks you very much for the replay.
const ensures that the data cannot be changed through the reference. These functions are read-only so you want to write const correct code so that accidental modification of the data will result in a compiler error.
OK.

Thanks
Topic archived. No new replies allowed.