Reputation: 33813
I have an error when create a simple linked list.
The error message is cannot convert 'listNode**' to 'listNode*' in assignment
.
The code:
struct listNode {
const char *data;
listNode *link;
};
int main(){
listNode *node = new listNode;
node->data = "Adley";
node->link = NULL;
listNode *node2 = new listNode;
node2->data = "Bert";
node2->link = NULL;
listNode *node3 = new listNode;
node3->data = "Colin";
node3->link = NULL;
node->link = &node2;
node2->link = &node3;
for(listNode *i = &node; i != NULL; i = i->link){
std::cout << i->data << std::endl;
}
return 0;
}
I know that this question is easy to solve it, but I'm sorry my brain is confused now.
Upvotes: 0
Views: 3125
Reputation: 71
for(listNode *i = node; i != NULL; i = i->link){
std::cout << i->data << std::endl;
}
you must use *i=node
NOT *i=&node
in the init-expression of the for
the type of i
: listNode*
the type of node
: listNode*
but the type of &node
: listNode**
Upvotes: 0
Reputation: 263260
Change:
node->link = &node2;
node2->link = &node3;
To:
node->link = node2;
node2->link = node3;
Remove the ampersands(&). node2
and node3
are already pointers. No need to point to pointers :)
Upvotes: 1