I am trying to overload the operator= in a template class.
I have this template class:
template <class T>
class Matrice
{
T m,n;
public:
template <class V>
friend Matrice<V>& operator=(const Matrice<V> &);
};
template <class T>
Matrice<T>& Matrice<T>::operator=(const Matrice<T> &M)
{
/*...*/
return *this;
}
and I also tried:
template <class T>
class Matrice
{
T m,n;
public:
template <class V>
Matrice<V>& operator=(Matrice<V> &);
};
template <class T>
Matrice<T>& operator=(Matrice<T> &M)
{
/*...*/
return *this;
}
but I still get this error:
error C2801: 'operator =' must be a non-static member
The bolded word is key here.
friendis not a member; it’s a friend. Remove thatfriendkeyword and treatoperator=as member:The syntactically proper version is:
Although I think that it’s wrong to use that
template <class V>there; the sematically proper version would beExplanation: you don’t generally want to assign
Type<V>toType<T>in this way; if you have to, then it is probably sign of bad design.