开发者

c2955 error on my Double Link List project

Okay, I'm making a project that implements a Double Linked List using classes, templates and structures. I constantly get the error:

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

when declaring the head and tail of a node in the LinkedList class.

DoubleLinkList.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 Nod开发者_高级运维e * tail; //C2955
public:
 bool push(T);
 //bool pop();
 //T at(); //C2146
 //bool clear();

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

#endif

DoubleLinkList.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()
//{
// 
//
//}

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


Error C2955 (link) relates to the absence of a type argument list to types that require one. In your code you reference the type Node which is actually a template and requires a type argument list. The fixes are below:

First of all, in DoubleLinkedList.h in the declaration of LinkedList (in the private: section at the top):

static Node * head;
static Node * tail;

should be (they should also not be declared static since I'm pretty certain each individual linked list needs its own head and tail):

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

since Node is actually the template class Node<T> and requires the type parameter itself.

Similarly in DoubleLinkedList.cpp in the push method:

Node * newN = new Node;

should be:

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

for the same reason.

Furthermore, template definitions should be defined in header files and the header files included using #include, e.g. #include "DoubleLinkedList.h", (and not compiled as you would .cpp files) since template expansion to produce concrete versions of the classes is performed by the preprocessor. Finally, there is also a problem with newN->pushMe; in your definition of LinkedList<T>::push: no such method exists. Fix these issues and there's a chance it might compile! Beyond that I don't vouch for the correctness of the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜