Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带模板的运算符重载赋值-C++;_C++_Templates_Operator Overloading_Operator Keyword - Fatal编程技术网

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;