我正在尝试添加它们在同一类中的两个对象。
在班级的私人部分,我有两个int
变量
class One {
private:
int num1, num2;
public:
One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object
friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
};
One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One
我不确定opeartor+
我猜有什么问题
One operator+(const One &a, const One &b){
One c,d,r;
c = a;
d = b;
r += b;
r += a;
return r;
}
我认为上面的代码是错误的,但我尝试像 b.num1 一样使用,我得到编译错误
error: 'int One::num1' is private
error: within this context
而且我也不能使用 b->num1 因为上面的函数不在成员函数部分。
error: base operand of '->' has non-pointer type 'const One'
这就是它的调用方式main
Result = LeftObject + RightObject;