C++ 带模板的运算符重载赋值-C++;
我是新来的(这实际上是我在这里的第一个问题),我正在为我的数据结构课程开发一个程序,希望能得到一些帮助C++ 带模板的运算符重载赋值-C++;,c++,templates,operator-overloading,operator-keyword,C++,Templates,Operator Overloading,Operator Keyword,我是新来的(这实际上是我在这里的第一个问题),我正在为我的数据结构课程开发一个程序,希望能得到一些帮助 它是关于lhs+=rhs行中的=、+和的运算符重载您希望编译器知道+=的含义。但是,您没有定义运算符+=,因此编译器不知道如何实现它。一种解决方案是首先定义操作符+=,并保持操作符+的原样。该操作符+有两个问题: template <class t_type> TLIST<t_type> & TLIST<t_type>::operator+(cons
它是关于
lhs+=rhs行中的=、+和的运算符重载
您希望编译器知道+=
的含义。但是,您没有定义运算符+=
,因此编译器不知道如何实现它。一种解决方案是首先定义操作符+=
,并保持操作符+
的原样。该操作符+
有两个问题:
template <class t_type>
TLIST<t_type> & TLIST<t_type>::operator+(const t_type &rhs)
{
TLIST <t_type> lhs;
lhs += rhs;
return *this;
}
如果加法的定义错误,则使用默认构造函数创建一个TLIST
,然后尝试添加参数。这不是添加到此对象,而是添加到空的TLIST
还有一个事实是,您的代码依赖于
运算符+=
,但该运算符似乎没有定义。首先,运算符+
应该是类TLIST的友元函数;其次,应定义运算符+=
TLIST<char> Char_List,TempChar1, TempChar2;
Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E'; // chaining
template <class t_type>
TLIST<t_type> & TLIST<t_type>::operator+(const t_type &rhs)
{
TLIST <t_type> lhs;
lhs += rhs;
return *this;
}
Error 1 error C2676: binary '+=' : 'TLIST' does not define this operator or a conversion to a type acceptable to the predefined operator c:\users\negri\dropbox\visual studio\projects\assignment 1 (tlist2)\assignment 1 (tlist2)\tlist.cpp 53 1 Assignment 1 (TLIST2)
template <class t_type>
TLIST<t_type> & TLIST<t_type>::operator+(const t_type &rhs)
{
TLIST <t_type> lhs;
lhs += rhs;
return *this;
}
return *this;