c++ - 我的双向链表项目出现 c2955 错误

标签 c++ class linked-list

好的,我正在制作一个使用类、模板和结构实现双链表的项目。 我经常收到错误:

doublelinklist.h(21) : error C2955: 'Node' : use of class template requires template argument list

在 LinkedList 类中声明节点的头部和尾部时。

双链表.h:

#ifndef DOUBLELINKLIST_H
#define DOUBLELINKLIST_H
#ifndef NULL
#define NULL 0
#endif
//#include <stdio.h>
//#include <string>

template <class T>
struct Node 
{
 T val;
 Node * next;
 Node * prev; //both of these are self-referential data types/structures
};

template <class T>
class LinkedList
{
private:
 static Node * head; //C2955
 static Node * tail; //C2955
public:
 bool push(T);
 //bool pop();
 //T at(); //C2146
 //bool clear();

 LinkedList()
 {
  /*static Node * */head = NULL;
  /*static Node * */tail = NULL;
 }
 ~LinkedList()
 {}
};

#endif

双链表.cpp

#include "DoubleLinkList.h"

template <class T>
bool LinkedList<T>::push(T pushMe)
{
 Node * newN = new Node;

 newN->next = NULL;
 newN->prev = NULL;

 if(this->head == NULL)
 {
  head = newN;
  head->val = pushMe;
  tail = newN;
  printf("the value in the head is %d\n", head->val);
  return true;
 }

 newN->prev = tail;
 tail->next = newN;
 newN->pushMe;
 tail = newN;

 printf("The value in the head is %d\n", head->val);
 return true;
}

//bool LinkedList::pop(int remove_where)
//{
// Node * toRemove = at(remove_where);
//
// if(toRemove == head)
// {
//  toRemove->next->prev = NULL;
//  head = toRemove->next;
// }
//
// else if(toRemove = tail)
// {
//  toRemove->prev->next = NULL;
//  tail = toRemove->prev;
// }
// 
// else
// {
//  toRemove->prev->next = toRemove->next;
//  toRemove->next->prev = toRemove->prev;
// }
//
// delete toRemove;
//
// return true;
//
//}
//
//T LinkedList::at()
//{
// 
// 
//}
//
//LinkedList::clear()
//{
// 
//
//}

主要.cpp

/*

1) Implement a Double-Linked List using templates and classes in C++.
   You may use the STL type "List" as a reference. 
   A) Don't forget to implement a NODE class...
   B) Don't forget to implement a class that is the actual list...
        i) You need to have AT LEAST:

            Push
            Pop
            At
            Clear 

2) Write a program that tests the functionality of your list class with the data types "int" and "std::string".
*/
#include <stdio.h>
#include <string>
#include "DoubleLinkList.h"

//template <class T>
int main()
{
    int x = 5;
    LinkedList<int> derp;
    derp.push(x);
    return 0;    
}

最佳答案

错误 C2955 ( link ) 与需要一个类型的类型参数列表的缺失有关。在您的代码中,您引用了类型 Node这实际上是一个模板,需要一个类型参数列表。修复如下:

首先,在DoubleLinkedList.hLinkedList 的声明中(在顶部的 private: 部分):

static Node * head;
static Node * tail;

应该是(它们也不应该声明为 static 因为我很确定每个单独的链表都需要自己的头和尾):

Node<T> * head;
Node<T> * tail;

Node实际上是模板类Node<T>并且需要类型参数本身。

DoubleLinkedList.cpp 类似在push方法:

Node * newN = new Node;

应该是:

Node<T> * newN = new Node<T>;

出于同样的原因。

此外,模板定义应该在头文件中定义,头文件包含使用#include ,例如#include "DoubleLinkedList.h" , (而不是像编译 .cpp 文件那样编译)因为生成类的具体版本的模板扩展是由预处理器执行的。最后,newN->pushMe;也有问题在你对 LinkedList<T>::push 的定义中: 没有这样的方法。解决这些问题,它就有可能编译!除此之外,我不保证代码的正确性。

关于c++ - 我的双向链表项目出现 c2955 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3780555/

相关文章:

c++ - Mac OS X 和静态 boost 库 -> std::string 失败

c++ - (C++) OOP - 用类扩展 - 初学者的困境

java - 在堆栈上创建字符链表?

c++ - 类函数与非类函数的名称冲突

java - 使用泛型创建类时出现 "> expected"错误

java列表通过重新排列链接来移动项目

java - java中的Hash Map与LinkedList

c++ - 如何使用 Qt 中的代码将小部件添加到中央小部件中

c++ - NetBeans 在编译 C++ 项目时无法执行 Perl 脚本

python - 类返回语句不打印任何输出