【问题标题】:error c2953: error while making double link list错误c2953:制作双向链表时出错
【发布时间】:2020-10-02 00:54:15
【问题描述】:

我有以下代码在“Node.cpp”文件中显示错误

#include "stdafx.h"
#include<iostream>

using namespace std;

template<typename X>//think error is here
class Node
{
    private:
        X data;
        Node *prev,*next;
    public:
        Node(X data)
        {
            this->data=data;
            prev=next=NULL;
        }
        ~Node(){}
        X getData()
        {
            return data;
        }   
};

这里是 DoubleList 文件

#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include "Node.cpp"

template<typename T>
class DoubleList
{
    private:
    public:
        Node *first, *last;

        DoubleList()
        {
            first=last=NULL;
        }
        ~DoubleList()
        {}
        void insertLast(T data)
        {
            Node *t=new Node(data)
            if(first==last)
            {
                 first=t;
            }
            last->next=t;
            t->prev=last;
            last=t;
            cout<<"\nNode having data ["<<data<<"] is inserted successfully";
        }

        void insertFirst(T data)
        {
            Node *t=new Node(data)
            if(first==last)
            {
                 first=t;
            }
            first->prev=t;
            t->next=first;
            first=t;
            cout<<"\nNode having data ["<<data<<"] is inserted successfully";
        }

        void display()
        {
            Node *t=first;
            cout<<"\nPrevious   Data    Next"
            while(t->next!=NULL)
            {
                cout<<t->prev<<"        "<<t->data<<"       "<<t->next<<endl;
                t=t->next;
            }
        }

};

我不知道为什么会收到此错误。我也检查了类似的问题,但他们没有解决我的问题。我试图避免头文件的混乱,我只创建了 cpp 文件。我试图使它们成为通用类型,但这个错误在我之上。我不知道该怎么办。

【问题讨论】:

    标签: c++ visual-c++ template-classes


    【解决方案1】:

    这会奏效。你有一些;失踪。您还应该使用 nullptr 而不是 NULL。您还缺少一些模板参数。

    节点.h

    #include "stdafx.h"
    template<typename X>
    class Node
    {
    private:
        X data;
        Node* prev, * next;
    public:
        Node(X data)
        {
            this->data = data;
            prev = next = nullptr;
        }
        ~Node() {}
        X getData()
        {
            return data;
        }
    };
    
    

    DoubleList.h

    #include "stdafx.h"
    #include <iostream>
    #include "Node.h"
    
    using namespace std;
    
    template<typename T>
    class DoubleList
    {
    private:
    public:
        Node<T>* first, * last;
    
        DoubleList()
        {
            first = last = NULL;
        }
        ~DoubleList()
        {}
        void insertLast(T data)
        {
            Node* t = new Node(data);
            if (first == last)
            {
                first = t;
            }
            last->next = t;
            t->prev = last;
            last = t;
            cout << "\nNode having data [" << data << "] is inserted successfully";
        }
    
        void insertFirst(T data)
        {
            Node* t = new Node(data);
            if (first == last)
            {
                first = t;
            }
            first->prev = t;
            t->next = first;
            first = t;
            cout << "\nNode having data [" << data << "] is inserted successfully";
        }
    
        void display()
        {
            Node* t = first;
            cout << "\nPrevious   Data    Next";
            while (t->next != NULL)
            {
                cout << t->prev << "        " << t->data << "       " << t->next << endl;
                t = t->next;
            }
        }
    };
    

    然后是 main.cpp

    #include "stdafx.h"
    #include<iostream>
    #include"DoubleList.h"
    
    using namespace std;
    
    int main()
    {
        DoubleList<int> list;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多