C++ Template Generika (template-argument-list)

Ich versuche zu implementieren ist eine Zirkuläre Doppelt verkettete Liste, und ich habe keine wahrscheinlich mit der LinkedList-Implementierung selbst. Das problem, das ich habe ist, ermöglicht es, generische Parameter mit Hilfe von Vorlagen. Ich habe mehrere tutorials den Umgang mit Vorlagen, aber ich habe nicht gefunden, etwas bestimmtes, was ich zu tun versuche.

Ich glaube, ich habe die meisten der Fehler, aber ich bin noch immer der Fehler:

linkedlist.h(37): error C2955: 'Node' : use of class template requires template argument list
linkedlist.h(9) : see declaration of 'Node'
main.cpp(6) : see reference to class template instantiation 'LinkedList<T>' being compiled
      with
      [
          T=int
      ]

Hier ist mein code:

LinkedList.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>

//node
template <class T>
class Node
{
private:

public:
    bool first;             //boolean tag 
    Node * next;            //pointer to the next node
    Node * prev;            //pointer to the prev node
    T data;                 //placeholder for generic data

    Node(T d);          //constructor
};

template <class T>
Node<T>::Node(T d)
{
    next = NULL;
    prev = NULL;
    data = d;
    first = false;
}

//a circular doubly-linked list
template <class T>
class LinkedList
{
private:

public:
    Node * p;                   //reference to the current node

    LinkedList();               //constructor

    bool empty();               //returns true if the list is empty, false otherwise
    int size();                 //returns the number of elements in the list    
    void insertBefore(T d); //inserts a node before the current node
    void insertAfter(T d);  //inserts a node after the current node
    void remove();              //removes the current node
    void moveAhead();           //moves to the next node
    void moveBack();            //moves to the previous node
    T access();             //returns the data of the current node
    void listContents();        //displays the data of every element in the list starting with the current
};

template <class T>
LinkedList<T>::LinkedList()
{
    p = NULL;
}

template <class T>
bool LinkedList<T>::empty()
{
    if (p == NULL)
    {
        std::cout << "List is Empty.\n";
        return true;
    }
    else
        return false;
}

template <class T>
int LinkedList<T>::size()
{
    if (p == NULL)
    {
        return 0;
    }
    if (p->next == p)
    {
        return 1;
    }
    else
        return 2; //placeholder
}

template <class T>
void LinkedList<T>::insertBefore(T d)
{
    Node *q, *t;
    if (p == NULL)
    {
        p = new Node<T>(d);
        p->next = p;
        p->prev = p;
        //std::cout << d << " inserted.\n";
    }
    else
    {
        if (p-> next == p)
        {
            q = new Node<T>(d);
            q->next = p;
            q->prev = p;
            p->next = q;
            p->prev = q;
            //std::cout << d << " inserted.\n";
        }
        else
        {
            q = p->prev;
            t = new Node<T>(d);
            p->prev = t;
            q->next = t;
            t->next = p;
            t->prev = q;
            //std::cout << d << " inserted.\n";
        }
    }
}

template <class T>
void LinkedList<T>::insertAfter(T d)
{
    Node *q, *t;
    if (p == NULL)
    {
        p = new Node<T>(d);
        p->next = p;
        p->prev = p;
        //std::cout << d << " inserted.\n";
    }
    else
    {
        if (p-> next == p)
        {
            q = new Node<T>(d);
            q->next = p;
            q->prev = p;
            p->next = q;
            p->prev = q;
            //std::cout << d << " inserted.\n";
        }
        else
        {
            q = p->next;
            t = new Node<T>(d);
            p->next = t;
            q->prev = t;
            t->next = q;
            t->prev = p;
            //std::cout << d << " inserted.\n";
        }
    }
}

template <class T>
T LinkedList<T>::access()
{
    if (p == NULL)
    {
        std::cout << "The list is empty. No data to be accessed.\n";
        return NULL;
    }
    else
        return p->data;
}

template <class T>
void LinkedList<T>::remove()
{
    if (p == NULL)
        std::cout << "The list is empty. No node exists to be removed.\n";
}

template <class T>
void LinkedList<T>::moveAhead()
{
    p = p->next;
}

template <class T>
void LinkedList<T>::moveBack()
{
    p = p->prev;
}

template <class T>
void LinkedList<T>::listContents()
{
    if (p == NULL)
    {
        std::cout << "This list is empty, there are no elements to be displayed.";
    }
    else
    {
        Node *q;
        p->first = true;;
        q = p;
        while (!q->next->first)
        {
            //std::cout << q->data << ", ";
            q = q->next;
        }
        //std::cout << q->data << ".\n";
        p->first = false;
    }
}

#endif

main.cpp:

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

int main()
{
    LinkedList<int> list;

    list.empty();
    std::cout << "p list size is: " << list.size() << std::endl;
    list.remove();
    list.access();
    list.insertBefore(3);
    list.insertBefore(2);
    list.moveBack();
    list.insertBefore(1);
    list.moveBack();
    list.moveAhead();
    list.moveAhead();
    list.insertAfter(5);
    list.insertAfter(4);
    list.moveBack();
    list.moveBack();
    list.listContents();

    system("PAUSE");
    return 0;
}

Soweit ich bin mir bewusst, dass ich noch nicht viel schlechtes getan, nur muss ich korrigieren, die paar Fehler. Wenn ich etwas grundlegend falsch, wenn könnten Sie bitte zeigen Sie es mir, oder mir direkt auf eine entsprechende Ressource, es wäre sehr geschätzt werden.

Vielen Dank im Voraus.

Schreibe einen Kommentar