我的链表中的错误 [英] Errors in my linked list

查看:147
本文介绍了我的链表中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    }
}





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)
{
}





#include "List.h"

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

    return 0;
}




感谢您的帮助!




Thanks for your help!!!

推荐答案

尝试在Node.h中向前声明列表:

Try forward declaring List in Node.h:

template <class Type> class List;



然后在Node< Type>中将其声明为朋友:



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

friend List<Type>;



干杯,

Ash



Cheers,

Ash


您可能想看看Sam Buss的出色链接列表.我在CP的CXMLProfile类中使用它.您可以从Sam的网站上下载它,网址为 http://www.math.ucsd.edu/~sbuss/MathCG/ [ ^ ].

单击"Ray Trace软件"的链接.在zip文件中,您将找到一个名为DataStructs的目录.您需要CLinkedList文件.
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.


这篇关于我的链表中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆