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.
nl_head
is not a pointer. try nl_head.prev
As suggested by the error message and your question title. nl_head
is not a pointer so you cannot use the ->
operator.
Make it a pointer. You will also need to allocate memory before you can use it.
Alternatively, you can not make it a pointer but instead use the dot operator to access its member.
nl_head
is an object of the node_list
structure, it is not a pointer, so use the dot operator and assign the loa:
nl_head.prev = &nl_head;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With