我正在这个程序中实现引用计数..
class plain_class{
public: int ref_count;
char *data;
plain_class(const char *);
~plain_class();
};
class ref_count{
private:
plain_class *p;
public:
ref_count(const char *);
ref_count(const ref_count &);
ref_count& operator=(const ref_count &);
void show_context();
~ref_count();
};
ref_count::ref_count(const char *str)
{
p=new plain_class(str);
}
ref_count::ref_count(const ref_count &ref)
{
p=ref.p;
p->ref_count++;
cout<<"reference count value="<<p->ref_count<<endl;
}
ref_count& ref_count::operator=(const ref_count &r1) //= operator overloaded function
{
if(*(this)==r1){
cout<<"Initializing to itself"<<endl;
return *this;
}
p=r1.p;
p->ref_count++;
cout<<"reference count value="<<p->ref_count<<endl;
return *this;
}
int main(int argc, char *argv[])
{
ref_count r1("Hello_world");
r1.show_context();
ref_count r2=r1;
r2=r2; //Error signature not matching to call = operator overload function
return 0;
}
没有特意写一些函数。
编译时出现此错误
In member function ‘ref_count& ref_count::operator=(const ref_count&)’:
no match for ‘operator==’ in ‘*(ref_count*)this == r1’
我以前的程序中总是这样写,但这不是编译..