1

ok, so here's my header file (or at least part of it):

template<class T>

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

and here is my .cc file:

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;
}

on the line List& List<T>::operator= (const List& other) I get the compilation error "Expected constructor, destructor, or type conversion before '&' token". What am I doing wrong here?

4

1 に答える 1

3

戻り値の型List&は、テンプレート引数なしでは使用できません。である必要がありますList<T>&

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


ただし、この構文エラーを修正した後でも、テンプレート関数の定義をヘッダー ファイルに配置する必要があるため、リンカーの問題が発生することに注意してください。テンプレートをヘッダー ファイルにしか実装できないのはなぜですか? を参照してください。詳細については。

于 2012-04-16T12:03:51.703 に答える