473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Question on the old problem about STL

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::operator==(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.
Jul 22 '05 #1
3 1274
* 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;

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
al***@start.no (Alf P. Steinbach) wrote in message news:<40****************@news.individual.net>...
* 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;


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?
Jul 22 '05 #3
* Prasanna:
al***@start.no (Alf P. Steinbach) wrote in message news:<40****************@news.individual.net>...
* 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;


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
1
by: Katrin Tomanek | last post by:
Hi, I tried ioctl with SIOCGSTAMP in order to get a timestamp for a received tcp packet. I tried before and after reading on the socket. But always I get a ENOENT error. I googled for this...
3
by: Andrea Giudici | last post by:
I'm quite new to C developing, and I cannot understand how to print out not only ASCII characters but also some colorful lines and circles! I'm currently using Miracle C compiler for Windows. ...
15
by: designconcepts | last post by:
bo'jour, bo'jour, So I have question to present to the forum about OOD. This is a Csharp forum, but C# is the lang of choice and the question is an exercise based on some comments by the chief...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.