读书人

模板中的operator,该如何解决

发布时间: 2012-02-04 15:43:09 作者: rapoo

模板中的operator
template <typename T>
class LeftHeap
{
public:
//...
const LeftHeap &operator=(const LeftHeap &rhs);

};

template <typename T>
const LeftHeap &LeftHeap <T> ::operator=(const LeftHeap <T> &rhs)
{
if( *this != rhs )
{
MakeEmpty();
root=Clone(rhs.root);
}
return *this;
}

错误提示:error C2955: 'LeftHeap ' : use of class template requires template argument list
\exercise\leftheap\leftheap\leftheap.h(45) : see declaration of 'LeftHeap '
\exercise\leftheap\leftheap\leftheap.h(178) : error C2244: 'LeftHeap <T> ::operator = ' : unable to match function definition to an existing declaration
\exercise\leftheap\leftheap\leftheap.h(23) : see declaration of 'LeftHeap <T> ::operator = '

要怎么解决?
谢谢

[解决办法]
C2244 can also occur when an incorrect function signature is used for a member function template.

内外定义和引用有区别。

[解决办法]
只觉得应该这样:
this != &rhs
而且, root 似乎是个指针, 好象有回收对象的问题.
其他没看出来.
[解决办法]
返回类型也要加 <T>
[解决办法]
if( *this != rhs ) //???, 这样还要定义!=, 而且比较的是内容

一般是这样的吧:
if (this != &rhs)

读书人网 >C++

热点推荐