【发布时间】:2013-01-29 23:17:41
【问题描述】:
#include <iostream>
class A
{
public:
int a;
A() { a = 2;}
A(int f) { a= f;}
void print() { std::cout << a << std::endl; }
};
class B
{
A a, at, at2;
A& operator += (A& b)
{
a.a = a.a + b.a;
return a;
}
public:
B(int a_, int at_, int at2_) : a(a_), at(at_), at2(at2_) {};
void update ()
{
a += at;
}
void printAll() { a.print(); at.print();}
};
int main()
{
B value ( 2, 3, 5);
value.printAll();
value.update();
value.printAll();
}
错误是:
temp.cpp:24:10: 错误:'((B*)this)->B::a += ((B*)this)->B::at 中的 'operator+=' 不匹配'
我做错了什么?
【问题讨论】:
-
您的重载占用了
B的左侧,而不是A。 -
有什么困惑?您显然没有为
A类定义operator+=...
标签: c++ overloading operator-keyword