我是 C++ 新手。
#include<cstdio>
#include<string>
using namespace std;
class add{
public :
int a,b;
add();
add(int ,int);
add operator+(add);
};
add::add():a(0),b(0){};
add::add(int x,int y):a(x),b(y){};
add add::operator+(add z)
{
add temp;
temp.a=a+z.a;
temp.b=b+z.b;
return temp;
}
int main()
{
add har(2,5),nad(3,4);
add total;
total=har+nad;
cout<< total.a << " "<<total.b;
return 0;
}
这个程序现在运行良好。但是,我之前写过
temp.a=this.a+z.a;
temp.b=this.b+z.b;
考虑到调用与编译时total=har+nad;
相同total=har.operator+(nad);
,显示错误。
operover1.cpp: In member function ‘add add::operator+(add)’:
operover1.cpp:22:14: error: request for member ‘a’ in ‘this’, which is of non-class type ‘add* const’
operover1.cpp:23:14: error: request for member ‘b’ in ‘this’, which is of non-class type ‘add* const’
为什么我们不能this.a+z.a
在这里使用?
有人请帮帮我。谢谢。