C ++模板泛型(模板参数列表) [英] C++ Template Generics (template argument list)
问题描述
我想实现一个循环双列表,我不可能与Linked List实现本身。我所遇到的问题是允许它使用模板采用通用参数。我已经检查了几个处理模板的教程,但我没有找到任何具体到我想要做什么。
我相信我已经解决了大部分的错误,但我仍然得到错误:
linkedlist.h(37):error C2955:'Node':use of class模板需要模板参数列表
linkedlist.h(9):参见'Node'的声明
main.cpp(6):参见类模板实例化'LinkedList< T& b with
[
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; //指向下一个节点的指针
Node * prev; //指向上一个节点的指针
T data; //通用数据的占位符
节点(T d); // constructor
};
template< class T>
Node< T> :: Node(T d)
{
next = NULL;
prev = NULL;
data = d;
first = false;
}
//圆形双向链表
template< class T>
class LinkedList
{
private:
public:
Node * p; //对当前节点的引用
LinkedList(); // constructor
bool empty(); //如果列表为空则返回true,否则返回false
int size(); //返回列表中元素的数量
void insertBefore(T d); //在当前节点之前插入一个节点
void insertAfter(T d); //在当前节点之后插入一个节点
void remove(); //删除当前节点
void moveAhead(); //移动到下一个节点
void moveBack(); //移动到上一个节点
T access(); //返回当前节点的数据
void listContents(); //显示列表中从当前
}开始的每个元素的数据;
template< class T>
LinkedList< T> :: LinkedList()
{
p = NULL;
}
template< class T>
bool LinkedList< T> :: empty()
{
if(p == NULL)
{
std :: cout< List is Empty.\\\
;
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>
p-> next = p;
p-> prev = p;
// std :: cout<< d<< inserted.\\\
;
}
else
{
if(p-> next == p)
{
q = new Node< T&
q-> next = p;
q-> prev = p;
p-> next = q;
p-> prev = q;
// std :: cout<< d<< inserted.\\\
;
}
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.\\\
;
}
}
}
template< class T>
void LinkedList< T> :: insertAfter(T d)
{
Node * q,* t;
if(p == NULL)
{
p = new Node< T>
p-> next = p;
p-> prev = p;
// std :: cout<< d<< inserted.\\\
;
}
else
{
if(p-> next == p)
{
q = new Node< T&
q-> next = p;
q-> prev = p;
p-> next = q;
p-> prev = q;
// std :: cout<< d<< inserted.\\\
;
}
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.\\\
;
}
}
}
template< class T>
T LinkedList< T> :: access()
{
if(p == NULL)
{
std :: cout< 列表为空,没有要访问的数据。
return NULL;
}
else
return p> data;
}
template< class T>
void LinkedList< T> :: remove()
{
if(p == NULL)
std :: cout< 该列表为空,没有要删除的节点。
}
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< 这个列表是空的,没有要显示的元素。
}
else
{
Node * q;
p-> first = true ;;
q = p;
while(!q-> next-> first)
{
// std :: cout< q-> data<< ,;
q = q-> next;
}
// std :: cout<< q-> data<< .\\\
;
p-> first = false;
}
}
#endif
main .cpp:
#include< iostream>
#includeLinkedList.h
int main()
{
LinkedList< int>列表;
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();
系统(PAUSE);
return 0;
}
据我所知,我没有做错太多,只需要纠正那些几个错误。如果我做了一些重大错误,如果你可以请指出我,或指示我一个适当的资源,这将是非常感激。
提前感谢。 / p>
错误是很清楚的。您在 LinkedList
中的多个位置使用 Node
,而不使用模板参数。
例如:
template< class T>
void LinkedList< T> :: insertBefore(T d)
{
Node * q,* t;
应为
template< class T>
void LinkedList< T> :: insertBefore(T d)
{
Node< T> * q,* t;
其他地方也是如此, c $ c> Node 类声明和 Node
函数定义(在函数定义中,必须有第一个函数定义) p>
I am trying to implement a Circular Doubly Linked List, and I have no probably with the Linked List implementation itself. The problem I am having is allowing it to take generic parameters using templates. I have checked several tutorials dealing with templates, but I haven't found anything specific to what I am trying to do.
I believe I have worked out most of the errors, but I am still getting the errors:
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
]
Here is my 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;
}
As far as I'm aware I haven't done much wrong, I just need to correct those few errors. If I have done something majorly wrong if you could please point it out to me, or direct me to an appropriate resource it would be much appreciated.
Thanks in advance.
The error is pretty clear. You are using Node
in several places inside LinkedList
without using it with template parameters.
For example:
template <class T>
void LinkedList<T>::insertBefore(T d)
{
Node *q, *t;
Should be
template <class T>
void LinkedList<T>::insertBefore(T d)
{
Node<T> *q, *t;
And the same goes for the other places, except inside the Node
class declaration and Node
function definitions (and in function definitions, you have to have it for the first one).
这篇关于C ++模板泛型(模板参数列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!