Compilation error "Global operator not found" for template overloaded operator

I have defined a template class and overloaded operators. When compiling, I get the following error message:

error C2677: binary '+ =': no ​​global operator found that accepts type 'Class' (or no acceptable conversion)

Here is the relevant class code:

template<int foo>
class Class
    int value;
    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, for example Class<3>

; operator +=

works great and does everything right.

However, if I have an object Class<3>

and one of Class<2>

, I get the above error, which points to a line where "+ =" is defined for T [the constructor for a different value of foo works fine and is also checked].

What am I doing wrong? How can I resolve this error? The operator is defined, just a few lines above.


source to share

2 answers

Assuming that the required constructor actually exists and works correctly, the error is in the code you posted,

this += Class(x);


which is trying to change the value of an immutable pointer this

. It should be

*this += Class(x);




I think there are two problems, as in this line:

this += Class(x);


One: the added object should be *this

instead this

, because the latter is a pointer and not the object itself.

Two: no conversion from the designer T

before Class

. That is, you cannot convert from Class<3>

to Class<2>

, so Class(x)

it won't compile. The solution is to add it:

template<int other> Class(const Class<other> &o)




All Articles