我正在从事C ++控制台应用程序课程
我试图创建一个链表
并且视觉工作室只给了我30多个错误,虽然代码对我来说似乎很好
什么是实际错误?
#include <iostream>
using namespace std;
template <class Nodetype>
class Node{
private:
Nodetype data;
Node<Nodetype>*next;
friend Linkedlist ;
};
template <class Nodetype>
class Linkedlist{
private:
Node<Nodetype>*head;
public:
Linkedlist();
void insertItem(Nodetype item);
void DeleteItem(Nodetype item);
void MakeEmpty();
bool FindItem(Nodetype item);
void Display();
};
template <class Nodetype>
Linkedlist<Nodetype>::Linkedlist()
{
head = NULL;
}
template <class Nodetype>
void Linkedlist< Nodetype >::insertItem(Nodetype item)
{
Node< Nodetype > *location;
location = new Node< Nodetype >();
location->data = item;
location->next = head;
head = location;
}
template <class Nodetype>
void Linkedlist<Nodetype>::Display()
{
Node<Nodetype>*current = head;
while (current != NULL)
{
cout << current->data;
current = current->next;
}
}
template <class Nodetype>
void Linkedlist< Nodetype >::DeleteItem(Nodetype item)
{
Node< Nodetype >* preLocation = NULL;
Node< Nodetype >* location = head;
if (item == location->data)
head = location->next;
else
{
do
{
preLocation = location;
location = location->next;
} while (item != location->data);
preLocation->next = location->next;
}
delete location;
}
template <class Nodetype>
void Linkedlist<Nodetype>::MakeEmpty()
{
Node<Nodetype>* tempPtr;
while (head != NULL)
{
tempPtr = head;
head = head > next;
delete tempPtr;
}
}
template <class Nodetype>
bool Linkedlist<Nodetype>::FindItem(Nodetype item)
{
bool found;
Node<Nodetype> *currentPos = head;
found = false;
while ((currentPos != NULL) && !found)
{
if (item == (currentPos->data))
found = true;
else
currentPos = currentPos->next;
}
return found;
}
void main(){
int x = 0;
Linkedlist<string> L;
L.insertItem("name1");
L.insertItem("name2");
cin >> x;
}
答案 0 :(得分:5)
并且视觉工作室只给了我30多个错误,虽然代码对我来说似乎很好
它不好,它充满了错误。
收听您的编译器。转到它告诉您有错误的行。修复它们。
例如:
head = head > next;
这显然是错的。解决它。
这不是有效的C ++:
friend Linkedlist ;
它应该是:
template<class T> friend class LinkedList;
但是,在你说它是朋友之前,你应首先声明LinkedList
。