'operator =' must be a member啥意思#includeclass String{public:String(char *str=NULL);friend void operator=(String&a,String&b);void show();private:char *name;};int main(){return 0;}编译提示:error C2801:'operator =' must be a member
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 08:49:45
'operator =' must be a member啥意思#includeclass String{public:String(char *str=NULL);friend void operator=(String&a,String&b);void show();private:char *name;};int main(){return 0;}编译提示:error C2801:'operator =' must be a member
'operator =' must be a member啥意思
#include
class String
{
public:
String(char *str=NULL);
friend void operator=(String&a,String&b);
void show();
private:
char *name;
};
int main()
{
return 0;
}编译提示:error C2801:'operator =' must be a member
'operator =' must be a member啥意思#includeclass String{public:String(char *str=NULL);friend void operator=(String&a,String&b);void show();private:char *name;};int main(){return 0;}编译提示:error C2801:'operator =' must be a member
=操作符重载函数不能为友元函数,只能为某类中的成员函数.
假如将赋值运算符重载为Dog类的友元:
friend Dog operator=( Dog &d1,Dog &d2)
{
d1.age = d2.age;
return d1;
}
例如有表达式 x = y;可以正确的将y赋值给x; x,y都是Dog类的对象.
又例如有一表达式99 = y;它被解释为operator=(99,y);
C++编译器将99转换为一个Dog类的对象(隐含的临时变量),然后使形参引用对象,因此这是个正确的表达式.但按C++的规定(99=y常量怎能赋值),这样的表达式应是错误的,为了保证与C++规定的赋值语义相一致,应将赋值运算符重载为类运算符.
同样,也应将+=,-=等这些赋值运算符重载为类运算符!