21

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.

3 Answers 3

20

nl_head is not a pointer. try nl_head.prev

1
  • I thought that it had to be '->' because 'prev' is a pointer. Although that did fix it so I was obviously wrong. Thank you :-) Dec 15, 2013 at 3:43
10

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.

2

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.