By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,504 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,504 IT Pros & Developers. It's quick & easy.

linked list code won't compile

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
* 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

P: n/a

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

P: n/a
* 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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.