linked list code won't compile

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

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
 
J

Jonathan Mcdougall

Martin said:
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
 
G

Greg

Martin said:
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
 
P

Phlip

Martin said:
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.
 
A

Alf P. Steinbach

* 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
 
G

Greg

Alf said:
* Martin Jørgensen:

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?



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
 
A

Alf P. Steinbach

* Greg:
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
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.
 
I

Ian Collins

Greg said:
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.
 
J

Jim Langston

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

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Greg said:
Martin Jørgensen wrote: -snip-



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
 
M

Marcus Kwok

Greg said:
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
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.std.c++/browse_frm/thread/93e1f28fa4058818

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

[0] http://groups.google.com/group/alt...._frm/thread/695389f94ecec593/4227caf0ee1452a2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top