1

I'm completely new to C++ and am breaking my head over a simple problem. I am trying to implement a simple linked list with three nodes. Here is my code:

#include<iostream> using namespace std; struct node(){ int data; struct node* next; }; struct node* BuildOneTwoThree() { struct node* head = NULL; struct node* second = NULL; struct node* third = NULL; head = new node; second = new node; third = new node; head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; return head; }; 

The question obviously being, why does it not compile? :(

Thank you in advance for any help!

5
  • 6
    What is the compiler error message? Commented Feb 9, 2012 at 19:07
  • 5
    If you're "completely new", then please expunge abusing namespace std; from your mind right now while you still have a chance, and never use it again. Commented Feb 9, 2012 at 19:10
  • Here is a copy of the error message(s): chopapp.com/#fq7vcb86 Commented Feb 9, 2012 at 19:10
  • @user1200428: I don't think that's the first error message you got. Commented Feb 9, 2012 at 19:17
  • If you can, use the standard library's std::list instead of creating your own. Commented Feb 9, 2012 at 19:19

4 Answers 4

6

Remove the "()" from the struct declarations. Your compiler should have told you that.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh well yes - glad to have helped, and please take Kerrek SB's advice seriously and remove the "using namespace std" while you still have the chance ;-)
1

Replace

struct node(){ int data; struct node* next; }; 

with

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

The extra paranthesis after the struct declaration are causing the error. The latter is the proper way of declaring a struct or a class in C++.

Comments

1

The declaration of your struct has an excess pair of parenthesis. When you remove them this this should be OK:

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

Comments

0

() should be written after functions instead of struct/class name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.