I have a program structured similiar to:
class A {
private:
std::set<B> members;
public:
void func(const C& arg) {
std::set<B>::iterator iter = members.find(a);
if(iter != members.end()) {
iter->check(arg);
}
}
}
class B {
private:
std::deque<C> writers;
public:
void check(const C& arg) {
if(std::find(writers.begin(), writers.end, arg) != writers.end()) {
/* Code */
}
}
}
class C {
private:
int id;
public:
bool operator==(const C& arg) {
return arg.id == this->id;
}
}
When I compile this, I get the following error message:
no matching function for call to ‘B::check(const C&) const’
note: candidates are: void B::check(const C&) <near match>
If I declare check() as const then the compiler throws an error demanding that the overloaded operator == in Class C to be declared as const. I don’t know if making the overloaded operator as const is the right thing to do. (I tried it once and as far as I can recollect it also gave some error).
I been trying to solve this for more than five days and still no leads.
The first thing is that
operator==should beconst. It does not modify data, nor should it ever unless you want to confuse your users. In general every function that need not modify the state of the object should beconstas to provide the maximal flexibility (i.e. allow calls through constant references).The same can be applied to
B::check, if it does only test and not modify, then it should beconst. And by extension inA::func, if it does not need to modify then it should be const.The error message will be slightly different in different compilers. In your case the compiler performed overload resolution and did not find a match to the call which is what it complains about:
Which indicates that it saw your member but discarded it as it needed a member function with the
consttag. In other compilers the error message will contain something on the lines of: callingvoid B::check(const C&)discards qualifiers which is a bit more convoluted and tries to say that calling a non-const member function on a const reference would require ignoring theconstqualifier.