私は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
の?
誰か助けてください。ありがとう。