2013-12-15 27 views
8

首先,代碼:基礎標識符具有非指針類型

// ... 

struct node_list { 
    node_list *prev; 
    node *target;  // node is defined elsewhere in the application 
    node_list *next; 
    }; 

node_list nl_head; 

int main() { 
    nl_head->prev = &nl_head; 
    // ... 
    return 0; 
    } 

我得到一個錯誤:

make (in directory: #####) 
g++ -Wall -std=c++11 -o main main.cc 
main.cc: In function ‘int main(int, char**)’: 
main.cc:38:9: error: base operand of ‘->’ has non-pointer type ‘node_list’ 
    nl_head->prev = &nl_head; 
     ^
Makefile:8: recipe for target 'main' failed 
make: *** [main] Error 1 
Compilation failed. 

至於我可以告訴我的語法是正確的。任何人都可以指出錯誤?

在任何人將其標記爲重複之前,我知道它類似於其他一些問題,但他們的解決方案似乎都不適用於我。除非我做錯了,我承認這是可能的,但這就是我來這裏的原因。

回答

8

正如錯誤消息和問題標題所示。 nl_head不是指針,因此您不能使用-­>運算符。

使它成爲一個指針。在使用之前,您還需要分配內存。

或者,您可以而不是使其成爲指針,而是使用點運算符來訪問其成員。

相關問題