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?

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
Johan Hjalmarsson
  • 3,433
  • 4
  • 39
  • 64
  • Template class definition must be in the header file. Have a look at this question for explanation: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Bojan Komazec Apr 16 '12 at 12:13

1 Answers1

3

The return type List& can't be used without the template arguments. It needs to be List<T>&.

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


But note that even after you fix this syntax error, you'll have linker problems because template function definitions need to be placed in the header file. See Why can templates only be implemented in the header file? for more information.

Community
  • 1
  • 1
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140