2009-05-28 5 views
0

내가 데이터 구조에 대한 책을 학습하고, 연결리스트의 예에서 자신의 노드를 준수하고, 나는이 오류가 나타납니다 오전 :LinkedList의 노드 +

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

노드의 코드는 다음과 같습니다

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; 

구조체에 익숙하지 않아서 Code :: 블록을 사용하여 컴파일하고 있습니다. 아무도 틀린 것을 아는가?

+0

나는이 데이터 구조의 저자를 C++로 신뢰할 수 있을지 확신하지 않는다. "struct Node *"("Node *"는 할 것이다)를 사용할 필요가 없으며, typedef 노드를 NODE로 사용할 필요가 없다. 구조체에 생성자가 있기 때문에 C와의 호환성을 위해 수행 할 수 없습니다. 이상한. 아마도 그것은 매우 오래된 것입니다. –

답변

5

코드 샘플이 잘못되었습니다. 생성자 선언 앞에 키워드 struct이 없어야합니다. 그것은이어야한다 :

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; 
관련 문제