链表代码将无法编译 [英] linked list code won't compile

查看:117
本文介绍了链表代码将无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我得到了这段代码,但我不会编译:


#include< iostream>

使用命名空间std;

////////////////////////////// //////////////////////////////////

struct link //列表的一个元素

{

int data; //数据项

link * next; //指向下一个链接

};

////////////////////////// //////////////////////////////////////

class linklist //一系列链接

{

private:

link * first; //指向第一个链接的指针

public:

linklist()//无参数构造函数

{first = NULL; } //没有第一个链接

void additem(int d); //添加数据项(一个链接)

void display(); //显示所有链接

};

// ------------------------- -------------------------------------

void linklist :: additem (int d)//添加数据项

{

link * newlink = new link; //建立新链接

newlink-> data = d; //给它数据

newlink-> next = first; //它指向下一个链接

first = newlink; //现在首先指向这个

}

// ------------------------ --------------------------------------

void linklist :: display()//显示所有链接

{

link * current = first; //将ptr设置为第一个链接

while(current!= NULL)//退出最后一个链接

{

cout<< current-> data<< ENDL; //打印数据

current = current-> next; //移动到下一个链接

}

}

////////////////// //////////////////////////////////////////////

int main()

{

linklist li; //制作链表

li.additem(25); //添加四个项目列表

li.additem(36);

li.additem(49);

li.additem(64 );


li.display(); //显示整个列表

返回0;

}


- - - - - - - - -


有9个错误但我认为一旦我得到ISO C ++禁止

声明''链接''没有类型错误消失,另一个会消失

也可能会消失。


我会说,因为struct link在顶部定义,编译器

应该没有什么可抱怨的。但显然它与我不同意

。如何让它运作?

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

---------------------------------------------- -----------------------------

Martin J?rgensen的故乡 - http://www.martinjoergensen.dk

推荐答案

Martin J?rgensen写道:
Martin J?rgensen wrote:


我得到了这段代码,但我不会编译:
#include< iostream>
使用命名空间std;
/////////////////////////////// /////////////////////////////////
struct link //列表中的一个元素
{
int数据; //数据项
链接*下一个; //指向下一个链接的指示
};
////////////////////////////////// //////////////////////////////
类链接列表//链接列表
{
私人:
链接*第一; //指向第一个链接的指针
public:
linklist()//无参数构造函数
{first = NULL; } //没有第一个链接
void additem(int d); //添加数据项(一个链接)
void display(); //显示所有链接
};
// --------------------------------- -----------------------------
void linklist :: additem(int d)//添加数据项
{
link * newlink = new link; //建立新链接
newlink-> data = d; //给它数据
newlink-> next = first; //它指向下一个链接
first = newlink; //现在首先指向这个
}
// -------------------------------- ------------------------------
void linklist :: display()//显示所有链接
{
链接* current = first; //将ptr设置为第一个链接
while(current!= NULL)//退出最后一个链接
{
cout<< current-> data<< ENDL; //打印数据
current = current-> next; //移动到下一个链接
}
} //////////////////////////////// //////////////////////////////////
int main()
{
linklist li; //制作链表

li.additem(25); //添加四个项目列表
li.additem(36);
li.additem(49);
li.additem(64);

li。显示(); //显示整个列表
返回0;
}
- - - - - - - - -

有9个错误,但我认为一次我得到了ISO C ++禁止
声明''链接''没有类型错误消失另一个也可能会消失。
Hi,

I got this piece of code, but I won''t compile:

#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
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;
}

- - - - - - - - -

There are 9 errors but I think once I get the "ISO C++ forbids
declaration of ''link'' with no type" error to disappear the other would
probably go away too.




这个确切的代码编译好了。这9个错误是什么?这是你用的确切代码吗?

Jonathan



This exact code compiles fine here. What are these 9 errors? Is this
the exact code you use?
Jonathan




Martin J?rgensen写道:

Martin J?rgensen wrote:


我得到了这段代码,但我不会编译:

#包括< iostream>
使用命名空间std;
///////////////////////////////// ///////////////////////////////
结构链接//列表中的一个元素
{
int data; //数据项
链接*下一个; //指向下一个链接的指示
};
////////////////////////////////// //////////////////////////////
类链接列表//链接列表
{
私人:
链接*第一; //指向第一个链接的指针
public:
linklist()//无参数构造函数
{first = NULL; } //没有第一个链接
void additem(int d); //添加数据项(一个链接)
void display(); //显示所有链接
};
// --------------------------------- -----------------------------
void linklist :: additem(int d)//添加数据项
{
link * newlink = new link; //建立新链接
newlink-> data = d; //给它数据
newlink-> next = first; //它指向下一个链接
first = newlink; //现在首先指向这个
}
// -------------------------------- ------------------------------
void linklist :: display()//显示所有链接
{
链接* current = first; //将ptr设置为第一个链接
while(current!= NULL)//退出最后一个链接
{
cout<< current-> data<< ENDL; //打印数据
current = current-> next; //移动到下一个链接
}
} //////////////////////////////// //////////////////////////////////
int main()
{
linklist li; //制作链表

li.additem(25); //添加四个项目列表
li.additem(36);
li.additem(49);
li.additem(64);

li。显示(); //显示整个列表
返回0;
}
- - - - - - - - -

有9个错误,但我认为一次我得到了ISO C ++禁止
声明''链接''没有类型错误消失另一个也可能会消失。

我会说,因为struct link在顶部定义,编译器不应该抱怨任何东西。但显然它不同意我的意见。如何让它工作?
Hi,

I got this piece of code, but I won''t compile:

#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
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;
}

- - - - - - - - -

There are 9 errors but I think once I get the "ISO C++ forbids
declaration of ''link'' with no type" error to disappear the other would
probably go away too.

I would say that since "struct link" is defined in the top, the compiler
shouldn''t have anything to complain about. But apparently it disagrees
with me. How to get it working?




结构名称链接与其他一些符号相冲突。所以我

将把这个名字大写:Link。


Greg



The struct name "link" is conflicting with some other symbol. So I
would capitalize the name: Link.

Greg


Martin J?rgensen写道:
Martin J?rgensen wrote:
struct link //列表中的一个元素
{
int data; //数据项
链接*下一个; //指向下一个链接的指针
};
struct link //one element of list
{
int data; //data item
link* next; //pointer to next link
};




此提示与您的问题无关,但是......


....所有这些评论都只是让工作。评论。他们让我想起了一个

小学生试图写一篇含有> 200字的文章。全部拿出来,

并让代码说明一切。


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - NOT博客!!!



This tip is not related to your problem, but...

....all these comments are just "make work" comments. They remind me of a
grade-schooler trying to write an essay with >200 words. Take them all out,
and let the code speak for itself.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


这篇关于链表代码将无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆