【问题标题】:class template, expected constructor, destructor类模板,预期的构造函数,析构函数
【发布时间】:2012-04-16 11:57:43
【问题描述】:

好的,这是我的头文件(或至少部分):

template<class T>

class List
{
public:
.
:
List& operator= (const List& other);
.
:
private:
.
:
};

这是我的 .cc 文件:

template <class T>
List& List<T>::operator= (const List& other)
{
    if(this != &other)
    {
        List_Node * n = List::copy(other.head_);
        delete [] head_;
        head_ = n;
    }
    return *this;
}

List&amp; List&lt;T&gt;::operator= (const List&amp; other) 行上,我收到编译错误“预期的构造函数、析构函数或在'&' 标记之前的类型转换”。我在这里做错了什么?

【问题讨论】:

标签: c++ class templates constructor


【解决方案1】:

没有模板参数就不能使用返回类型List&amp;。必须是List&lt;T&gt;&amp;

template <class T>
List<T>& List<T>::operator= (const List& other)
{
   ...
}


但是请注意,即使您修复了这个语法错误,您也会遇到链接器问题,因为模板函数定义需要放在头文件中。请参阅Why can templates only be implemented in the header file? 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2020-07-21
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多