Question on the old problem about STL

P

Prasanna

Hi,

I am enountering this problem when compiling my code. error C2678:
binary '==' : no operator defined which takes a left-hand operand of
type 'const class whatever'

I have the template class defined like this:

template<unsigned p> class GF{
public:
...
bool operator==(const GF<p>&);
unsigned& element();
...
private:
....
elm;
....

};

template<unsigned p>
bool GF::eek:perator==(const GF<p>& gfe)
{
if(elm==gfe.element())
return true;
return false;
}

template<unsigned p>
unsigned& GF::element()
{
return &elm; <---- i am returning the reference
}

I have one more class which has a vector<GF<2> > as its data member.
And i do an operation like this:

vector1 == vector2 where both are vector<GF<2> >.

I read the previous posts in this group and the solution for this
problem is said as making the == operator aa const. Like

boool operator == (const GF<p>&) const;

But if i do that, i get the following compiler error. error C2662:
'element' cannot convert 'this' pointer from 'const class GF<2>' to
'class GF<2>&'

May be the problem is because my element() function is returning the
reference. But i want it that way. How do i solve this?

Thanks.
Prasanna.
 
A

Alf P. Steinbach

* Prasanna:
Hi,

I am enountering this problem when compiling my code. error C2678:
binary '==' : no operator defined which takes a left-hand operand of
type 'const class whatever'

I have the template class defined like this:

template<unsigned p> class GF{
public:
...
bool operator==(const GF<p>&);

Should be 'const'.

bool operator==( const GF& ) const;

unsigned& element();

You also need a 'const' version.

unsigned const& element() const;
 
P

Prasanna

* Prasanna:

Should be 'const'.

bool operator==( const GF& ) const;



You also need a 'const' version.

unsigned const& element() const;

This solves the problem. But i am also using something like this:
gfe.element() = 10; and if i incorporate the above solution i get the
following error. "error C2166: l-value specifies const object."
What do i do for that? Should i change the organization of my template
class(like making it a non-template class!) or is there another way?
 
A

Alf P. Steinbach

* Prasanna:
This solves the problem. But i am also using something like this:
gfe.element() = 10; and if i incorporate the above solution i get the
following error. "error C2166: l-value specifies const object."
What do i do for that? Should i change the organization of my template
class(like making it a non-template class!) or is there another way?

It seems to have nothing to do with templates, only with constness.

Presumably the statement "gfe.element() = 10;" is within a 'const'
member function.

You shouldn't be changing the object in a 'const' function, since
'const' promises not to change the object.
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top