I've defined a template class and overloaded operators. When compiling, I get the following error message:
error C2677: binary '+=' : no global operator found which takes type 'Class' (or there is no acceptable conversion)
Here is the relevant code of the class:
template<int foo>
class Class
{
private:
int value;
public:
template<int other_foo>
friend class Class;
// Constructors and copy constructors for several data type
// including other possibilities of "foo"; all tested.
Class& operator+=(const Class& x)
{
value += x.value;
return *this;
}
template<class T>
Class& operator+=(const T& x)
{
this += Class(x);
return *this;
}
};
If I create two objects of, e.g., Class<3>; the operator += works fine and does the right thing.
However, if I have an object of Class<3> and one of Class<2>, I get the above error which points at the line where "+=" is defined for T [the constructor for different value of foo works fine and is also tested].
What am I doing wrong? How can I resolve this error? The operator is defined, just a few lines above.