Program gives error on Ubuntu but runs fine on Windows

Hey guys, when i run the following code on ubuntu(code::blocks) it gives me a number of errors but when i run it on windows(code::blocks) it works fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

struct link
{
     int data;
     link *next;
};

class linklist
{
     private:
          link *first;
     public:
          linklist()
          {
               first=NULL;
          }
          void display();
          void additem(int d);
};

void linklist:: display()
{
     link *current=first;
     while(current!=NULL)
     {
          cout << current->data << endl;
          current=current->next;
     }
}

void linklist:: additem(int d)
{
     link *current = new link();
     current->data = d;
     current->next = first;
     first = current;
}

int main()
{
     linklist a ;
     a.additem(5);
     a.display();
     return 0;
}


Thanks.
What are the errors? My ESP is not working today.
here are the errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
line14 error:ISO C++ forbids declaration of 'link' with no type

line14 error: expected ';' before '*' token

line18 error: 'first' was not declared in this scope

line26 error: 'current' was not declared in this scope

line26 error: 'first' was not declared in this scope

line36 error: 'current' was not declared in this scope

line36 error: expected type-specifier before 'link'

line36 error: expected ';' before 'link'

line36 error: 'first' was not declared in this scope
you have to put a struct with each declaration ithink.

like

struct link *first;
or you can do this:

1
2
3
4
5
typedef struct link
{
     int data;
     link *next;
}link;
I don't really believe he's getting those errors with that code. What he posted is valid C++ and those errors are nonsense.

the whole typedef'ing the struct thing would be a solution for C, but since this is clearly C++ it shouldn't make a difference.

I suspect he's not compiling the code he thinks he is. Maybe there are subtle differences between what he compiled on Windows and what he compiled on Ubuntu.
@Disch: yup, these are non sense but i am getting the errors even if compile the exactly same program from the terminal, and have done it a number of times.

Ihtisham wrote:
@Disch: yup, these are non sense but i am getting the errors even if compile the exactly same program from the terminal, and have done it a number of times.


Are you compiling exactly what you posted "here" with both compilers and getting those errors?
Yes, the same i have posted here.
yes.. these will come for C. And he must be compiling for C but looks c++.

on windows if the file is .cpp, it will compile fine. What about ubuntu? could you give the compile command you are using? are you sure you are including iostream?
The file is .cpp in both the places, and using the usual command gcc -o linklist linklist.cpp .
what happens if you put this then run on g++ im just guessing btw so dont yell at me how stupid it is.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

struct link; // add this. allow to see declaration of link before link *next is used

struct link
{
     int data;
     link *next;
};
Last edited on
Correct me if I'm wrong, but shouldn't you be using g++ and not gcc to compile C++ code?
Last edited on
you are right it and i think he is. that was just my error and confusion of the two
classic!!
closed account (z05DSL3A)
You have a name clash. Rename link or put it in a namespace.
Last edited on
Finally it worked thanks alot Grey Wolf, and hey can you explain why there was a clash in ubuntu not in windows?

@Xander314: Yes, you are right g++ should be used because it links to the standard C++ library, but sometimes gcc also works. ;)
closed account (z05DSL3A)
Ihtisham wrote:
can you explain why there was a clash in ubuntu not in windows?

There is a *nix API call called link (for creating hard links in the file system) that is not there on windows.
Topic archived. No new replies allowed.