Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'operator ==' not implemented - oh yes it is!

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
At last!
A C++ problem!
I have a class (called Tdb_field) with the following operator both defined & implemented:

virtual bool operator==(Tdb_field& rhs);

This works just fine. However, another fully defined & implemented has the compiler telling me that it hasn't been implemented:

virtual bool operator==(long& rhs);

The line that throws the error is:

if(!(fld==(long)12345))
ShowMessage("operator== error");

I know that operators are sometimes defined as friends & stuff, so could it be that? I'm afraid I know nothing about the friend keyword or it's effects.

Any & all help gratefully received!
Cheers,
Douglas J. Livesey

Common sense is what tells you the world is flat.
 
if(fld != (long)12345)
ShowMessage("operator== error");

maybe :)

tomcruz.net


 
I'll try it, but I think that that would ultimately cause the same problem, as I would first have to define & implement the != operator along similar lines to the == operator.
I reckon that'll just be the same story.
I have to admit, I've never really sussed alot of the operator stuff. Or friend functions, for that matter.
Cheers,
DJL

Common sense is what tells you the world is flat.
 
To further clarify, consider the two operator functions below for Tdb_field:

//BEGIN .h FILE SAMPLE

class Tdb_field
{
public:
//constructors & other stuff

virtual bool operator==(Tdb_field& rhs); //fine
virtual bool operator==(long& rhs); //trouble maker

protected:
long Fdata;
//misc stuff
};
//END .h FILE SAMPLE

//BEGIN .cpp FILE SAMPLE


bool Tdb_field::eek:perator==(Tdb_field& rhs) //fine
{
//code returning true or false - no problem
}

bool Tdb_field::eek:perator==(long& rhs) //trouble maker
{
if(Fdata==val)return true;
else return false;
}
//END .cpp FILE SAMPLE

The first works just fine, the second screws up. I've cast to long & everything, so I can't figure it.
Cheers,
DJL

Common sense is what tells you the world is flat.
 
Damn'! Got it!
There was no hint of 'const' in the error message, so I didn't think to see if I was calling the operator function with a const long. Shouldn't it warn you about this?
Anyway, that's what it was!
Still don't get friend functions, though.
Cheers,
DJL

Common sense is what tells you the world is flat.
 
the != operator is already defined and using it merely calls the default. There is no need to overload it. I dont think that overloading an operator in this manner neccessarily negates the default. the compiler chooses the corect method based upon the parameter list passed to the operator. I would hope someone would clarify this if I am not understanding it correctly.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top