我正在尝试在模板类中重载operator=。
我有这个模板类:
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;
}我也试过了:
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;
}但是我仍然得到这个错误:
error C2801: 'operator =' must be a non-static member发布于 2012-06-24 03:49:00
您混合了朋友和成员的声明和定义:在第一个示例中,您将operator=声明为朋友,但将其定义为类成员。在第二个示例中,您将operator=声明为成员,但尝试将其定义为非成员。您的operator=必须是成员(请参阅this question为什么),并且您可以执行以下操作:
template <class T>
class Matrice
{
T m,n;
public:
Matrice<T>& operator=(Matrice<T> &);
};
template <class T>
Matrice<T>& Matrice<T>::operator=(Matrice<T> &M)
{
/*...*/
return *this;
}https://stackoverflow.com/questions/11172389
复制相似问题