Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include "Node.h"
template<class Type>                           //Error  4   error C3857: 'List': multiple template parameter lists are not allowed

class List
{

public:
    List();
    ~List();
    void AddNode(void);


private:
    Node<Type>* m_pNode;

};                                            //Error   3   error C2989: 'List' : class template has already been declared as a non-class template


template<class Type>
List<Type>::List():m_pNode(NULL)       //Error   5   error C2988: unrecognizable template declaration/definition       //Error   6   error C2059: syntax error : '<'


{
}



template<class Type>
List<Type>::~List()                    //Error  7   error C2588: '::~List' : illegal global destructor    //Error   8   fatal error C1903: unable to recover from previous error(s); stopping compilation

{
    Node<Type>* pNode = NULL;

    while (m_pNode != NULL)
    {
        pNode = m_pNode;
        m_pNode = m_pNode->m_pNext;
        delete pNode;
    }
    m_pNode = NULL;
}

template<class Type>
void List<Type>::AddNode( void )
{
    if (m_pNode == NULL)
    {
        m_pNode = new Node<Type>();
    }
    else
    {
        Node* pTailNode = m_pNode;

        while (pTailNode->m_pNext != NULL)
        {
            pTailNode = pTailNode->m_pNext;
        }

        pTailNode->m_pNext            = new Node<Type>();
        pTailNode->m_pNext->m_pBefore = pTailNode;

    }
}


C++
template<class Type>
class Node
{
    friend class List<Type>; // Error   1   error C2059: syntax error : '<'

public:
    Node ();               //Error  2   error C2238: unexpected token(s) preceding ';'

private:
    Type  m_nData;
    Node<Type>* m_pNext;
    Node<Type>* m_pBefore;
};

template<class Type>
Node<Type>::Node() : m_pNext(NULL),m_pBefore(NULL)
{
}


C++
#include "List.h"

int _tmain(int argc, _TCHAR* argv[])
{
    List<int> myList;
    myList.AddNode();

    return 0;
}



Thanks for your help!!!
Posted
Updated 6-Jun-10 5:02am
v2
Comments
Moak 6-Jun-10 11:04am    
Updated subject, tags and formatting. I am pretty sure I have seen this question previously today, wonder where it went?

Try forward declaring List in Node.h:

template <class Type> class List;


and then declare it as a friend in Node<Type>:

friend List<Type>;


Cheers,

Ash
 
Share this answer
 
Comments
jc24 6-Jun-10 7:32am    
Thank you very much
You may want to take a look at Sam Buss' excellent linked list. I use it in my CXMLProfile class here on CP. You can download it from Sam's site at http://www.math.ucsd.edu/~sbuss/MathCG/[^].

Click on the link for "Ray Trace software". In the zip file you will find a directory called DataStructs. You want the CLinkedList file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900