I want to create an unique id for each instance of a class, that cannot be modified.
Then I want to create an operator ==
such that p1==p2
returns true
if p1 and p2 have the same id, i.e. are the same element.
The way I plan to do it is:
parent.hpp
class parent{
public:
...
int parent::GetUid() const;
private:
static int newUID;
const int uid;
}
parent.cpp
bool operator ==(const parent p1, const parent p2)
{
return (p1.GetUid()== p2.GetUid());
}
parent::parent()
:uid(newUID++){}
int parent::newUID=0;
int parent::GetUid() const
{
return uid;
}
But though I do initialize newUID
, I get the following error:
error c2789 an object of const-qualified type must be initialized
EDIT: the above error is solved and occurred because of a typo I made.
Now when I try to use the operator I get the following error:
error C2676: binary '==' : 'parent' does not define this operator or a conversion to a type acceptable to the predefined operator
I get this error when I do the following:
parent p1;
parent p2;
...
if(p1==p2){
t=0;
}