FatalKeystroke
FatalKeystroke

Reputation: 3042

base operand of ‘->’ has non-pointer type

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.

Upvotes: 22

Views: 100431

Answers (3)

user97975
user97975

Reputation: 27

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;

Upvotes: 2

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6039

nl_head is not a pointer. try nl_head.prev

Upvotes: 20

ApplePie
ApplePie

Reputation: 8942

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.

Upvotes: 10

Related Questions