0
class CFruit { 
private: 
 string m_name; 
public: 

 string getName() const; 

 CFruit(string name = "NoName"); 
};

The fruitsalad is represented in the class CFruitSalad:

class CFruitSalad { 
//Overloaded Operators 
friend ostream& operator <<(ostream& out, const CFruitSalad& f); 
friend CFruitSalad operator +(const CFruit& f1, const CFruit& f2); 

private: 
 string m_fruitsalad; 
public: 

 CFruitSalad(string content = ""); 
 string getName() const; 
};

Now when I use write this:

 CFruit f1("Apple"); 
 CFruit f2("Orange"); 
 CFruit f3 ("Banana"); 
 CFruitSalad fs; 
 fs = f1 + f2 + f3; //this line generates the error 
 cout << "Content: " << fs << endl; 

When compiled the program the following error is received: error C2678: binary '+' : no operator found which takes a left-hand operand of type 'CFruitSalad' (or there is no acceptable conversion)

Why does this error occur and how to solve it?

4 Answers4

1
  fs = f1 + f2 + f3; 

No matter the order of evaluation (assume f2+ f3 is evaluated at first), it will first return a object of CFruitSalad based on your declaration here:

friend CFruitSalad operator +(const CFruit& f1, const CFruit& f2);

Now, for the next + operation, you are adding an object of CFruit with an object of CFruitSalad, you don't have an overloaded version that takes one CFruit and one CFruitSalad, which causes the error.

You need to change the implementation of the overloaded operator+ (Don't overload operators if it is not really meaningful IMHO).

taocp
  • 23,276
  • 10
  • 49
  • 62
0

Your + operator is only defined between two instances of class CFruit. You need to either add another operator + for CFruitSalad and CFruit, or use a converting constructor (see the checked answer).

Community
  • 1
  • 1
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
0

fs = f1 + f2 + f3; //this line generates the error

Okay, so order of operations time.

f1 + f2 calls operator+ and returns a CFruitSalad object, which we will call _f4.

Then _f4 + f3 happens, which is trying to call operator+ (CFruitSalad const &, CFruit const &), causing your error.

QuestionC
  • 10,006
  • 4
  • 26
  • 44
0

You defined operator + as

friend CFruitSalad operator +(const CFruit& f1, const CFruit& f2); 

that is as having return type CFruitSalad

Binary operator + is evaluared left to right. So in this statement

fs = f1 + f2 + f3;

after evaluating f1 + f2 you get a temporary object of type CFruitSalad and try to add it with object f3 that has type CFruit. However you did not define operator + that has the left operand of type CFruitSalad and the right operand of type CFruit and there is no conversion function that could convert an object of one type to another.

So the compiler issues the error.

You could declare the operator as

friend CFruitSalad operator +(const CFruitSalad& f1, const CFruit& f2); 

Or could make it a member function of class CFruitSalad. For example

CFruitSalad operator +( const CFruit& f2) const; 

Also there would be a sense to declare operator += instead of the operator + For example

CFruitSalad & operator +=( const CFruit& f2); 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335