views:

159

answers:

1

I am learning a book on data structures, and complied their node in linked list example, and I receive this error:

 and Everything.cpp|7|error: expected unqualified-id before "int"|
 and Everything.cpp|7|error: expected `)' before "int"|
||=== Build finished: 2 errors, 0 warnings ===|

The code for the node is:

typedef struct Node
{
    struct Node(int data)    //Compile suggest problem is here
    {
        this-> data = data;
        previous = NULL;
        next = NULL;
    }
    int data;
    struct Node* previous;
    struct Node* next;
} NODE;

I am not familiar with structs and I am using Code::blocks to compile. Does anyone know whats wrong?

+5  A: 

The code sample is wrong. There should not be the keyword struct in front of the constructor declaration. It should be:

typedef struct Node
{
    Node(int data)  // No 'struct' here
    {
        this-> data = data;
        previous = NULL;
        next = NULL;
    }
    int data;
    struct Node* previous;
    struct Node* next;
} NODE;
Adam Rosenfield