C++ '->' 的基操作数具有非指针类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20590884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
base operand of ‘->’ has non-pointer type
提问by FatalKeystroke
First, the code:
首先,代码:
// ...
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;
}
I get an error:
我收到一个错误:
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.
As far as I can tell my syntax is correct. Can anyone point out the error?
据我所知,我的语法是正确的。谁能指出错误?
Before anyone flags it as a duplicate, I am aware it is similar to a couple other questions but none of their solutions seem to work for me. Unless I'm doing it wrong, which I'll admit is possible, but that's why I'm here.
在任何人将其标记为重复之前,我知道它与其他几个问题类似,但他们的解决方案似乎都不适合我。除非我做错了,我承认这是可能的,但这就是我在这里的原因。
采纳答案by edtheprogrammerguy
nl_head
is not a pointer. try nl_head.prev
nl_head
不是指针。尝试nl_head.prev
回答by ApplePie
As suggested by the error message and your question title. nl_head
is not a pointer so you cannot use the -->
operator.
正如错误消息和您的问题标题所建议的那样。nl_head
不是指针,因此您不能使用-->
运算符。
Make it a pointer. You will also need to allocate memory before you can use it.
使它成为一个指针。您还需要先分配内存,然后才能使用它。
Alternatively, you can notmake it a pointer but instead use the dot operator to access its member.
或者,您不能将其设为指针,而是使用点运算符来访问其成员。