473,748 Members | 7,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::addit em(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::displ ay() //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 2012
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::addit em(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::displ ay() //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::addit em(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::displ ay() //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::addit em(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::displ ay() //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*********@sp am.jay.net> wrote in message
news:ed******** ****@news.tdc.d k...
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::addit em(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::displ ay() //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

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

Similar topics

1
2046
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 root pointer) not be null. /* LINKED LIST DEFINITIONS */ typedef struct a_fnode {
57
4293
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
9
2258
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
3216
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 than one element to the list. Is this a problem with the function of the internal class Node ? Thanks for any help. Here is the code: #include<iostream>
17
3044
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 else? The reason for this question is that I had an assignment in which I was
4
1629
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 { protected: struct node { char* data; node* prev; node* post;
12
4049
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 the shared memory. I run fedora 9 and gcc 4.2. I am able to insert values in to the list, remove values from the list, but the problem is in traversing the list. Atlease one or 2 list nodes disappear when traversing from the base of the list or...
11
2550
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
7
5772
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.