Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

pointers

g++

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.

like image 995
FatalKeystroke Avatar asked Dec 15 '13 03:12

FatalKeystroke


3 Answers

nl_head is not a pointer. try nl_head.prev

like image 94
edtheprogrammerguy Avatar answered Nov 19 '22 23:11

edtheprogrammerguy


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.

like image 9
ApplePie Avatar answered Nov 19 '22 23:11

ApplePie


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;
like image 1
user97975 Avatar answered Nov 19 '22 22:11

user97975