c++visual-c++template-classes

error c2953: error while making double link list


i have following code which is showing error in "Node.cpp" file

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

here is DoubleList file

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

};

i dont know why i am getting this error. i have checked similar questions also but they didn't resolved my issue. I am trying to avoid the mess of header files that why i have only created cpp files. I am trying to make them generic type but this error is above me. I dont know what todo of it.


Solution

  • This will work. You had some ; missing. You should also be using nullptr instead of NULL. You were also missing some template paramaters.

    Node.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;
            }
        }
    };
    

    And then main.cpp

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