Reputation: 1112
I was reading a block of codes from a C++ book, and trying to compile it using g++
Here was the error: main.cpp:11:3: error: ‘link’ does not name a type main.cpp: In constructor ‘linklist::linklist()’: main.cpp:15:4: error: ‘first’ was not declared in this scope
So it seems like the linklist class doesnt recognize the link struct, why?
code:
#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 !!!!!!!!Here is the first error ~!!!!!!!!!!!
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;
}
Upvotes: 0
Views: 213
Reputation: 158599
As far as I can tell you are probably building this as C code, if I build using g++
it works just fine but if I use gcc
the first error is:
error: 'link' does not name a type
Edit
So I can reproduce this on g++
by adding:
#include <unistd.h>
So it looks like the name link
conflicts with something in that header file. Renaming link
to another name seems to fix the issue.
Upvotes: 0
Reputation: 1397
The problem is that the name link is conflict with a function named link in unistd.h. Try with g++ on the OS X.
Upvotes: 2