473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

linked list code won't compile

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?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 6 '06 #1
10 1981
Martin Jørgensen wrote:
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.


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

May 6 '06 #2

Martin Jørgensen wrote:
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?


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

Greg

May 6 '06 #3
Martin Jørgensen wrote:
struct link //one element of list
{
int data; //data item
link* next; //pointer to next link
};


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!!!
May 6 '06 #4
* Martin Jørgensen:

I got this piece of code, but I won't compile:
Although from a strictly a formal point of view it shouldn't, it
compiles fine with MSVC 7.1, MingW g++ 3.4.4, and Comeau Online 4.3.3.

Which compiler are you using?

#include <iostream>
Here you need to add

#include <ostream>

to be formally correct (very compilers require it, though).

using namespace std;
////////////////////////////////////////////////////////////////
struct link //one element of list
{
int data; //data item
link* next; //pointer to next link
};
As Phlip mentioned, remove those comments.

Then you'll see that the name "link" is not well chosen: it doesn't
document what it is.

Rename to "Node".

////////////////////////////////////////////////////////////////
class linklist //a list of links
{
private:
link* first; //pointer to first link
public:
linklist() //no-argument constructor
{ first = NULL; } //no first link
Use constructor initializer list.

void additem(int d); //add data item (one link)
void display(); //display all links
Applying mechanical cookbook-like guidelines, the "display" function
should be 'const'.

Applying common sense, it should not be a member of the class (never do
i/o in a non-i/o class, except for debugging).

When you move it outside, as a non-friend, you'll find that the class is
"incomplete" in the functionality it offers: more must be added in order
to be able to implement "display" as a non-friend non-member.

};
//--------------------------------------------------------------
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;
Not necessary. The default is to return 0 from main. If concerned
about correct return value from "main", instead concentrate on catching
possible exceptions, and in case of exception, returning EXIT_FAILURE.
}


And again, those comments are more to write, more to read, can not be
checked by the compiler, do not contribute anything... Remove. :-)

Hth.,

- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 6 '06 #5

Alf P. Steinbach wrote:
* Martin Jørgensen:

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


Although from a strictly a formal point of view it shouldn't, it
compiles fine with MSVC 7.1, MingW g++ 3.4.4, and Comeau Online 4.3.3.

Which compiler are you using?

#include <iostream>


Here you need to add

#include <ostream>

to be formally correct (very compilers require it, though).


No compiler will require including <ostream> once <iostream> has been
included.

Since a C++ library header must include any other headers needed for
any definitions that that header uses, we can be certain that
<iostream> will always include <ostream> on its own - because without
<ostream>, std::cout et al could not be defined.

And certainly the numerous code examples found both in the Standard and
in many of the C++ commitee's working group papers - all of which
include <iostream> without ever including <ostream> - could not all be
in error. And in fact they are not.

Greg

May 6 '06 #6
* Greg:
Alf P. Steinbach wrote:
* Martin Jørgensen:
I got this piece of code, but I won't compile: Although from a strictly a formal point of view it shouldn't, it
compiles fine with MSVC 7.1, MingW g++ 3.4.4, and Comeau Online 4.3.3.

Which compiler are you using?

#include <iostream>

Here you need to add

#include <ostream>

to be formally correct (very compilers require it, though).


No compiler will require including <ostream> once <iostream> has been
included.


That is incorrect.

Since a C++ library header must include any other headers needed for
any definitions that that header uses, we can be certain that
<iostream> will always include <ostream> on its own - because without
<ostream>, std::cout et al could not be defined.
That is incorrect, both in the logic (lack of logic) and in the implied
fact of definition: <iostream> does not provide definitions.

And certainly the numerous code examples found both in the Standard and
in many of the C++ commitee's working group papers - all of which
include <iostream> without ever including <ostream> - could not all be
in error. And in fact they are not.


In fact those examples are in error.

Don't speak about your hunches as if they're facts.

They're not.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 6 '06 #7
Greg wrote:
Alf P. Steinbach wrote:
* Martin Jørgensen:
I got this piece of code, but I won't compile:


Although from a strictly a formal point of view it shouldn't, it
compiles fine with MSVC 7.1, MingW g++ 3.4.4, and Comeau Online 4.3.3.

Which compiler are you using?
#include <iostream>


Here you need to add

#include <ostream>

to be formally correct (very compilers require it, though).

No compiler will require including <ostream> once <iostream> has been
included.

Since a C++ library header must include any other headers needed for
any definitions that that header uses, we can be certain that
<iostream> will always include <ostream> on its own - because without
<ostream>, std::cout et al could not be defined.

Are you sure?

I thought there wasn't any such guarantee. I've been caught out by
missing headers swapping standard libraries.

--
Ian Collins.
May 6 '06 #8

"Martin Jørgensen" <un*********@spam.jay.net> wrote in message
news:ed************@news.tdc.dk...
Hi,

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

#include <iostream>
using namespace std;
This is probably your problem right here. There is most likely a link class
or object inside of iostream that you are bringing into the unnamed
namespace by using namespace std;

The simplest way to fix this problem is to remove this statement all
together. Which means you'll need to say
std::cout and std::endl
If you don't want to do that, change this statement to only bring in what
you actually need.
using std::cout;
using std::endl;
then you won't even have to do that.

This is why I, and some others, consider "using namespace" evil.

////////////////////////////////////////////////////////////////
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?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

May 6 '06 #9
Greg wrote:
Martin Jørgensen wrote:

-snip-
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?

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


Wow... You were right.... Thanks for the other suggestions in the other
replies. I read them and considered them.

Compiler is g++.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 6 '06 #10
Greg <gr****@pacbell.net> wrote:

Alf P. Steinbach wrote:
* Martin J?rgensen:
> #include <iostream>
Here you need to add

#include <ostream>

to be formally correct (very compilers require it, though).


No compiler will require including <ostream> once <iostream> has been
included.


Except (at least) HP aCC. I used std::endl in a program, and I got a
compilation error until I explicitly #include'd <ostream> as well as
<iostream>.
Since a C++ library header must include any other headers needed for
any definitions that that header uses, we can be certain that
<iostream> will always include <ostream> on its own - because without
<ostream>, std::cout et al could not be defined.
There is no guarantee that any standard library header includes any
other header.
And certainly the numerous code examples found both in the Standard and
in many of the C++ commitee's working group papers - all of which
include <iostream> without ever including <ostream> - could not all be
in error. And in fact they are not.


However, many people (including Dr. Stroustrup [0]) believe this to be a
defect. Rumor has it that a defect report was filed, but I have not
been able to find it.

See:
http://groups.google.com/group/comp....e1f28fa4058818

and the first topic in:
http://en.wikipedia.org/wiki/Talk:C++

[0] http://groups.google.com/group/alt.c...27caf0ee1452a2

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
9
by: Erik | last post by:
Hi, i have this struct and this linked list /* structure describing a book.*/ typedef struct { char code; char author; char title; int year; int reserved; } Book;
4
by: bejiz | last post by:
Hello, I have written a short program for practising linked lists. But there is surely something wrong for when I compile there is a unhandled exception and it does not print if I try to add more...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
4
by: Jonas Ferreira | last post by:
Hi everyone, I'm trying to code a linked list example and everything was working fine. Then I started to code my clear() (to clear my list) but it won't work. Here is the code: class list {...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.