我正在阅读 C++ 书中的一段代码,并尝试使用 g++ 对其进行编译
这是错误: main.cpp:11:3: error: 'link' 没有命名类型 main.cpp: 在构造函数中'linklist::linklist()': main.cpp:15:4: error: 'first ' 在这方面没有申明
所以看起来链接列表类无法识别链接结构,为什么?
代码:
#include <iostream>
using namespace std;
struct link //one element of list
{
int data; //data item
link* next; //pointer to next link
};
class linklist //a list of links
{
private:
link* first; //pointer to first link !!!!!!!!Here is the first error ~!!!!!!!!!!!
public:
linklist() //no-argument constructor
{
first = NULL;
} //no first link
void additem(int d); //add data item (one link)
void display(); //display all links
};
void linklist::additem(int d) //add data item
{
link* newlink = new link; //make a new link
newlink->data = d; //give it data
newlink->next = first; //it points to next link
first = newlink; //now first points to this
}
void linklist::display() //display all links
{
link* current = first; //set ptr to first link
while( current != NULL ) //quit on last link
{
cout << current->data << endl; //print data
current = current->next; //move to next link
}
}
int main()
{
linklist li; //make linked list
li.additem(25); //add four items to list
li.additem(36);
li.additem(49);
li.additem(64);
li.display(); //display entire list
return 0;
}