由于某些原因,我在编译代码时不断收到“模板agrument 1无效错误”。我不知道为什么。我正在尝试使用模板创建链接列表。这是我的代码
运行我的main方法时,我将类引入:
Dlist<L> list;
然后我尝试实现一些功能。
我得到的错误是:
Dlist<L> list; template agrument 1 is invalid.
^
HELP
ListNode.H
#ifndef LISTNODE_H
#define LISTNODE_H
#include <iostream>
using namespace std;
template <class L>
class ListNode
{
public:
L data;
ListNode *prev;
ListNode *next;
ListNode(L d);
ListNode();
~ListNode();
};
template <class L>
ListNode<L>::ListNode()//blank constructor
{
}
template <class L>
ListNode<L>::ListNode(L d)//overloaded constructor
{
next = NULL;
prev = NULL;
data = d;
}
template <class L>
ListNode<L>::~ListNode()//deconstructor
{
next = NULL;
prev = NULL;
}
#endif
DList.h
#ifndef DLIST_H
#define DLIST_H
#include <iostream>
#include "ListNode.h"
using namespace std;
template <class L>
class Dlist
{
private:
ListNode<L>* front;
ListNode<L>* back;
unsigned int size;
public:
Dlist();//constructor
~Dlist();//destructor
//insert and remove functions
void insertFront(L data);
int removeFront();
void insertBack(L data);
int removeBack();
ListNode<L>* removeAt(L pos);
//other functions
bool isEmpty();
int find(L val);
int getFront();
int getBack();
unsigned int getSize();
void printList();
};
template <class L>
Dlist<L>::Dlist()
{
size = 0;
front = NULL;
back = NULL;
}
template <class L>
Dlist<L>::~Dlist()
{
if(front != NULL)
{
delete front;
}
}
template <class L>
void Dlist<L>::insertFront(L data)
{
ListNode<L> *node = new ListNode<L>(data);
if (size = 0)
{
back = node;
}
else
{
front->prev=node;
node->next=front;
}
size++;
front = node;
}
template <class L>
int Dlist<L>::removeFront()
{
ListNode<L> *temp = front;
if(front->next = NULL)
{
back = NULL;
}
else
{
front->next->prev = NULL;
front->next = NULL;
}
front = front->next;
int val = temp->data;
delete temp;
--size;
return val;
}
template <class L>
void Dlist<L>::insertBack(L data)
{
ListNode<L> *node = new ListNode<L>(data);
if (size == 0)
{
front = node;
}
else
{
back->next = node;
node->prev = back;
}
back = node;
size++;
}
template <class L>
int Dlist<L>::removeBack()
{
ListNode<L> *temp = back;
if(back->prev = NULL)
{
front = NULL;
}
else
{
back->prev->next = NULL;
}
back = back->prev;
int val = temp->data;
delete temp;
--size;
return val;
}
template <class L>
ListNode<L>* Dlist<L>::removeAt(L key)
{
ListNode<L>* curr = front;
while(curr->data != key)
{
curr=curr->next;
if (curr == NULL)
{
return NULL;
}
}
//found the key
if(curr == front)
{
front=curr->next;
}
else
{
curr->prev->next = curr->next;
}
if(curr==back)
{
back = curr->prev;
}
else
{
curr->next->prev = curr->prev;
}
curr->next=NULL;
curr->prev = NULL;
size--;
return curr;
}
template <class L>
unsigned int Dlist<L>::getSize()
{
return size;
}
template <class L>
void Dlist<L>::printList()
{
ListNode<L>* curr = front;
while(curr != NULL)
{
cout<< curr->data << endl;
curr = curr->next;
}
}
template <class L>
int Dlist<L>::getFront()
{
return front;
}
template <class L>
int Dlist<L>::getBack()
{
return back;
}
#endif
答案 0 :(得分:1)
你不能在尝试时使用模板类。
DLIST&LT; L&GT;列表;
由于L不是有效的数据类型。有效数据类型包括内置或用户定义的数据类型。 &#34; L&#34;您正在将其用作模板,因此您只能在模板类中使用它。
如果您定义自己的班级,则上述声明有效。&#39; L&#39;如下所示:
class L
{
int a;
};
希望有所帮助。