0

I have written a "Node" template class for a doubly linked list.

Header file:

template< class T >
class Node
{
public:
    Node();
    ~Node();

    Node<T>* GetNext();
    Node<T>* GetPrev();

    void SetNext( Node<T>* pNode );
    void SetPrev( Node<T>* pNode );

    void SetData( T& data );
    T*  GetData();

private:
    Node<T>*    p_Next;
    Node<T>*    p_Prev;
    T*      p_Data; 
};



template< class T >
Node<T>::Node() : p_Next(NULL), p_Prev(NULL), p_Data(NULL)
{

}

//======================================================================================

template< class T >
Node<T>::~Node()
{
    if( p_Data != NULL)
        delete p_Data;

    p_Next = NULL;
    p_Prev = NULL;
}

//======================================================================================

template< class T >
Node<T>* Node<T>::GetNext()
{
    return p_Next;
}

//======================================================================================

template< class T >
Node<T>* Node<T>::GetPrev()
{
    return p_Prev;
}

//======================================================================================

template< class T >
void Node<T>::SetData( T& data )
{
    if(p_Data == NULL)
        p_Data = new T;

    *p_Data = data;
}

//======================================================================================

template< class T >
T*  Node<T>::GetData()
{
    return p_Data;
}

When I try to compile it Visual Studio gives me the following syntax errors in the line that has the constructor implementation.

error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '<'
error C2588: '::~Node' : illegal global destructor
fatal error C1903: unable to recover from previous error(s); stopping compilation

I cannot identify any syntax errors as mentioned above. Please help me, what am I doing wrong? Thanks in advance.

5
  • Compiles fine on VS2010. Commented Apr 2, 2013 at 8:43
  • 2
    Is this the entire header? I bet there must be some additional code, such as header guards or include directives. The code you gave compile perfectly (after including <cstddef> or another header defining NULL), so the problem must be in some code that you have not posted. My bet would be that you forgot a semi-colon after a class definition that is included before Node, but this is a wild guess. Commented Apr 2, 2013 at 8:43
  • Also, you should use const T& instead of T& in SetData(). Commented Apr 2, 2013 at 8:45
  • Compiles fine on Qt Creator too. Commented Apr 2, 2013 at 8:57
  • I used VS2008. It does compile fine in 2010. Commented Apr 2, 2013 at 10:12

1 Answer 1

1

It looks like you haven't included some definition for NULL.

Please consider to using nullptr instead. In VS2010 you might need to add a definition for that, but when changing to a fully C++11 compatible compiler, you will be using the C++ keyword that has been designed for that purpose.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.