Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板泛型(模板参数列表)_C++_Templates_Generics_Compiler Errors_Linked List - Fatal编程技术网

C++ C++;模板泛型(模板参数列表)

C++ C++;模板泛型(模板参数列表),c++,templates,generics,compiler-errors,linked-list,C++,Templates,Generics,Compiler Errors,Linked List,我正在尝试实现一个循环双链表,而我可能对链表实现本身没有任何了解。我遇到的问题是允许它使用模板获取通用参数。我已经查阅了一些关于模板的教程,但是我没有找到任何具体的内容 我相信我已经解决了大部分错误,但我仍然在犯错误: linkedlist.h(37): error C2955: 'Node' : use of class template requires template argument list linkedlist.h(9) : see declaration of 'Node' ma

我正在尝试实现一个循环双链表,而我可能对链表实现本身没有任何了解。我遇到的问题是允许它使用模板获取通用参数。我已经查阅了一些关于模板的教程,但是我没有找到任何具体的内容

我相信我已经解决了大部分错误,但我仍然在犯错误:

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
      ]
linkedlist.h(37):错误C2955:“节点”:使用类模板需要模板参数列表
h(9):参见“节点”的声明
cpp(6):请参阅对正在编译的类模板实例化“LinkedList”的引用
具有
[
T=int
]
这是我的密码:

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
\ifndef LINKEDLIST\u H
#定义链接列表
#包括
//节点
模板
类节点
{
私人:
公众:
bool-first;//布尔标记
Node*next;//指向下一个节点的指针
Node*prev;//指向prev节点的指针
T data;//通用数据的占位符
节点(td);//构造函数
};
模板
Node::Node(td)
{
next=NULL;
prev=NULL;
数据=d;
第一个=假;
}
//循环双链表
模板
类链接列表
{
私人:
公众:
Node*p;//对当前节点的引用
LinkedList();//构造函数
bool empty();//如果列表为空,则返回true,否则返回false
int size();//返回列表中的元素数
void insertBefore(td);//在当前节点之前插入一个节点
void insertAfter(td);//在当前节点之后插入节点
void remove();//删除当前节点
void moveAhead();//移动到下一个节点
void moveBack();//移动到上一个节点
T access();//返回当前节点的数据
void listContents();//显示列表中每个元素的数据,从当前
};
模板
LinkedList::LinkedList()
{
p=零;
}
模板
bool链接列表::empty()
{
if(p==NULL)
{
std::cout next==p)
{
返回1;
}
其他的
返回2;//占位符
}
模板
void LinkedList::insertBefore(td)
{
节点*q,*t;
if(p==NULL)
{
p=新节点(d);
p->next=p;
p->prev=p;
//std::cout next=p;
q->prev=p;
p->next=q;
p->prev=q;
//标准::cout-prev=t;
q->next=t;
t->next=p;
t->prev=q;
//标准::cout-prev=p;
//std::cout next=p;
q->prev=p;
p->next=q;
p->prev=q;
//std::cout next=t;
q->prev=t;
t->next=q;
t->prev=p;
//std::cout next->first)
{
//std::cout数据下一步;
}
//std::cout data first=false;
}
}
#恩迪夫
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;
}
#包括
#包括“LinkedList.h”
int main()
{
链接列表;
list.empty();

std::cout错误很明显。您在
LinkedList
中的多个位置使用
Node
,而没有将其与模板参数一起使用

例如:

template <class T>
void LinkedList<T>::insertBefore(T d)
{
    Node *q, *t;
模板
void LinkedList::insertBefore(td)
{
节点*q,*t;
应该是

template <class T>
void LinkedList<T>::insertBefore(T d)
{
    Node<T> *q, *t;
模板
void LinkedList::insertBefore(td)
{
节点*q,*t;

其他地方也一样,除了
节点
类声明和
节点
函数定义(在函数定义中,第一个必须有它).

错误很明显。您在
链接列表
中的多个位置使用了
节点
,而没有将其与模板参数一起使用

例如:

template <class T>
void LinkedList<T>::insertBefore(T d)
{
    Node *q, *t;
模板
void LinkedList::insertBefore(td)
{
节点*q,*t;
应该是

template <class T>
void LinkedList<T>::insertBefore(T d)
{
    Node<T> *q, *t;
模板
void LinkedList::insertBefore(td)
{
节点*q,*t;

其他地方也一样,除了
节点
类声明和
节点
函数定义(在函数定义中,第一个必须有它).

好了,一切都搞定了。我对你急需的脑袋上的伤疤感激不尽。好了,一切都搞定了。我对你急需的脑袋上的伤疤感激不尽。