重载运算符“=”和“+”
Overloading operators '=' and '+'
我正在学习如何使用模板以及如何重载运算符。我已经设法重载了 operator[]
,但是我遇到了重载 operator+
和 operator=
的问题。这是我的代码:
template <class T>
class A
{
public:
//...
friend A<T>& A<T>::operator+ (A<T>&, const A<T>&);
friend A<T>& A<T>::operator= (A<T>&, const A<T>&);
};
template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right)
{
//some functions
return left;
}
template<class T> A<T>& A<T>::operator= (A<T>& left, const A<T>& right)
{
//some functions
return left;
}
每当我尝试编译时,我都会遇到这些错误:
'+': is not a member of 'A<T>'
'=': is not a member of 'A<T>'
'operator =' must be a non-static member
我做错了什么?
编辑:
我已经成功更新了代码:
template <class T>
class A
{
public:
//...
A<T> operator+ (A<T>);
A<T> operator= (A<T>, const A<T>);
};
template<class T> A<T> A<T>::operator+ (A<T> right)
{
//some functions
return *this;
}
template<class T> A<T> operator= (A<T> right)
{
//some functions
return *this;
}
看起来 operator+
现在工作正常,但编译器给出了这个错误:
'operator=' must be a non static member
为什么它是静态成员,我该如何解决?
对于初学者来说,赋值运算符必须是非静态成员函数
来自 C++ 标准(13.5.3 赋值)
1 An assignment operator shall be implemented by a non-static member
function with exactly one parameter. Because a copy assignment
operator operator= is implicitly declared for a class if not declared
by the user (12.8), a base class assignment operator is always hidden
by the copy assignment operator of the derived class.
其次(11.3好友)
1 A friend of a class is a function or class that is given permission
to use the private and protected member names from the class. A class
specifies its friends, if any, by way of friend declarations. Such
declarations give special access rights to the friends, but they do
not make the nominated friends members of the befriending class.
例如这个定义
template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right)
^^^^^
{
//some functions
return left;
}
不正确。至少你应该删除 A<T>::
因为运算符不是 class.
的成员
作为非静态成员实现的运算符必须只接受 1 个输入参数,即右侧操作数。左侧操作数是调用运算符的 this
对象。
作为静态成员或非成员实现的运算符必须接受 2 个输入参数,即左侧操作数和右侧操作数。
您的 operator=
被声明为具有 2 个输入参数的非静态成员,这是错误的。
此外,operator+
意味着 return 一个新对象,它是两个输入对象的副本加在一起。不要 return 对调用运算符的对象的引用。而 operator=
是指 return 对要分配给的对象的引用。
试试这个:
template <class T>
class A
{
public:
//...
A<T> operator+(const A<T>&) const;
A<T>& operator=(const A<T>&);
};
template<class T> A<T> A<T>::operator+(const A<T>& right) const
{
A<T> result(*this);
//some functions to add right to result as needed...
return result;
}
template<class T> A<T>& A<T>::operator=(const A<T>& right)
{
// some functions to copy right into this...
return *this;
}
我正在学习如何使用模板以及如何重载运算符。我已经设法重载了 operator[]
,但是我遇到了重载 operator+
和 operator=
的问题。这是我的代码:
template <class T>
class A
{
public:
//...
friend A<T>& A<T>::operator+ (A<T>&, const A<T>&);
friend A<T>& A<T>::operator= (A<T>&, const A<T>&);
};
template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right)
{
//some functions
return left;
}
template<class T> A<T>& A<T>::operator= (A<T>& left, const A<T>& right)
{
//some functions
return left;
}
每当我尝试编译时,我都会遇到这些错误:
'+': is not a member of 'A<T>'
'=': is not a member of 'A<T>'
'operator =' must be a non-static member
我做错了什么?
编辑:
我已经成功更新了代码:
template <class T>
class A
{
public:
//...
A<T> operator+ (A<T>);
A<T> operator= (A<T>, const A<T>);
};
template<class T> A<T> A<T>::operator+ (A<T> right)
{
//some functions
return *this;
}
template<class T> A<T> operator= (A<T> right)
{
//some functions
return *this;
}
看起来 operator+
现在工作正常,但编译器给出了这个错误:
'operator=' must be a non static member
为什么它是静态成员,我该如何解决?
对于初学者来说,赋值运算符必须是非静态成员函数
来自 C++ 标准(13.5.3 赋值)
1 An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8), a base class assignment operator is always hidden by the copy assignment operator of the derived class.
其次(11.3好友)
1 A friend of a class is a function or class that is given permission to use the private and protected member names from the class. A class specifies its friends, if any, by way of friend declarations. Such declarations give special access rights to the friends, but they do not make the nominated friends members of the befriending class.
例如这个定义
template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right)
^^^^^
{
//some functions
return left;
}
不正确。至少你应该删除 A<T>::
因为运算符不是 class.
作为非静态成员实现的运算符必须只接受 1 个输入参数,即右侧操作数。左侧操作数是调用运算符的 this
对象。
作为静态成员或非成员实现的运算符必须接受 2 个输入参数,即左侧操作数和右侧操作数。
您的 operator=
被声明为具有 2 个输入参数的非静态成员,这是错误的。
此外,operator+
意味着 return 一个新对象,它是两个输入对象的副本加在一起。不要 return 对调用运算符的对象的引用。而 operator=
是指 return 对要分配给的对象的引用。
试试这个:
template <class T>
class A
{
public:
//...
A<T> operator+(const A<T>&) const;
A<T>& operator=(const A<T>&);
};
template<class T> A<T> A<T>::operator+(const A<T>& right) const
{
A<T> result(*this);
//some functions to add right to result as needed...
return result;
}
template<class T> A<T>& A<T>::operator=(const A<T>& right)
{
// some functions to copy right into this...
return *this;
}