A problem with "const" when overloading operator ==

P

Paulo da Silva

Please consider the following fragment:

using namespace std;

class C1
{ public:
bool operator == (C1 const &s)
{ return false;
}
};

class C2
{ private:
C1 c;
public:
bool comp(C2 const * s) const
{ return this->c==s->c;
}
};

I got the following error
"passing ‘const C1’ as ‘this’ argument of ‘bool C1::eek:perator==(const
C1&)’ discards qualifiers"

at line "{ return this->c==s->c;".

Is there any way to fix the problem acting on operator == definition, or
in the class C1?

Thanks.
 
Ö

Öö Tiib

Please consider the following fragment:

using namespace std;

class C1
{       public:
                bool operator == (C1 const &s)

Should be:
                bool operator == (C1 const &s) const
 
P

Paulo da Silva

Em 06-12-2010 00:02, Öö Tiib escreveu:
Should be:
bool operator == (C1 const &s) const
Oops! I tried that, but unfortunately I forgot to change the prototype
of the original program (.h). The error changed but I didn't see that
because the very few error lines available on the eclipse do not have
attracted my attention.

I didn't program in c++ for the last couple of years and I am making
trivial mistakes :-( .

Thank you very much and sorry for the trivial question.

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top