' - >'的基本操作数具有非指针类型

时间:2013-12-15 03:28:02

标签: c++ pointers g++

首先,代码:

// ...

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.

据我所知,我的语法是正确的。任何人都可以指出错误吗?

在任何人将其标记为副本之前,我知道它与其他几个问题相似,但他们的解决方案似乎都不适合我。除非我做错了,我承认这是可能的,但这就是我在这里的原因。

3 个答案:

答案 0 :(得分:10)

nl_head不是指针。试试nl_head.prev

答案 1 :(得分:9)

根据错误消息和您的问题标题的建议。 nl_head不是指针,因此您无法使用-­>运算符。

使它成为一个指针。您还需要在使用之前分配内存。

或者,您可以使其成为指针,而是使用点运算符来访问其成员。

答案 2 :(得分:0)

pandasnl_head 结构的对象,它不是指针,因此使用点运算符并分配 loa:

node_list