一,代码:
// ... 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.
据我所知,我的语法是正确的.有谁可以指出错误?
在任何人将其标记为重复之前,我知道它与其他几个问题相似,但他们的解决方案似乎都不适合我.除非我做错了,我承认这是可能的,但这就是我在这里的原因.
nl_head
不是指针.尝试nl_head.prev
正如错误消息和您的问题标题所示.nl_head
不是指针,因此您无法使用->
运算符.
把它变成指针.您还需要先分配内存,然后才能使用它.
或者,您不能使它成为指针,而是使用点运算符来访问其成员.