0

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?

1
  • I'm not sure I trust the author of this data structures book with C++. There's no need to use "struct Node*" ("Node*" will do), or to typedef Node as NODE. It can't have been done for compatibility with C, because the struct has a constructor. Odd. Perhaps it's very old. Commented May 28, 2009 at 10:10

1 Answer 1

5

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.