I'm trying to add two object that they are in the same class.
In the private section of the class I have two int variables
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
I'm not sure I have a problem on the opeartor+ I guess
One operator+(const One &a, const One &b){
One c,d,r;
c = a;
d = b;
r += b;
r += a;
return r;
}
I think the above code is wrong, but I tried to use like b.num1 and I get compile error
error: 'int One::num1' is private
error: within this context
and I can't use b->num1 as well because the above function is not in the member function section.
error: base operand of '->' has non-pointer type 'const One'
This is how it calls in main
Result = LeftObject + RightObject;
operator+is wrong ? BTW: you forget to initialize the members.b.num1,num1is a private member. If you need to access it you need to have a getter function for that.b->is invalid as you have not created a pointer type. Do you intended to add the private integers?roperator +add? To get private create a public function which returns the private member maybe -public: inline int getInt1(){return int1;}operator+it suppose to add two objects together and assign it to object callrthen return that to the main function in main the calling of this operator is like thisResult = Left + right;