Click to See Complete Forum and Search --> : Templates and classes


theBUSH
April 5th, 2003, 10:02 PM
Could someone tell me why this code...

list.hh
#ifndef _LIST_HH
#define _LIST_HH

template <class T>
class list
{
private:
class node
{
T m_data;
node *m_prev;
node *m_next;
};

public:
list();

private:
node m_base;
node *m_head;
};

#endif // _LIST_HH

list.cc
#include "list.hh"

list::list()
: m_head(m_base)
{
}

gives this error...

list.cc:3: syntax error before `::' token

im using GNU C++ 3.2.2

Paul McKenzie
April 5th, 2003, 10:18 PM
The list (a bad name to begin with) is a template class. Therefore all of your list member functions need the "template <class T>" placed before it.

template <class T> // <--This is missing
list<T>::list() : m_head(m_base) { } //<<--Look at this line also

Also, there is a std::list class already available in the C++ standard library that is a linked list class. There is no need (unless this is a class exercise) to reinvent the wheel.

This is why the name you chose, "list", will confuse others, thinking that it is the std::list class. It may also cause problems if you ever do introduce any functions that are in the "std" namespace. Either change the name of the class, or put it in a namespace, or do the wise thing and use what the standard library already provides -- std::list.

Regards,

Paul McKenzie