简体   繁体   中英

cannot initialize a variable of type 'Node *' with an rvalue of type 'const Node *'

What is the problem here, &head should yield a pointer to a Node and that is the type of cur but apparently there is a type mismatch?

I am trying to print the elements of a linked list using its head

void printList(const Node& head){
  Node* cur = &head;
  cout<<"[ ";
  while(!(cur==0)){
    cout<<cur->x<<" ";
    cur = cur->next;
  }
  cout<<"]"<<endl;
}

cannot initialize a variable of type 'Node *' with an rvalue of type 'const Node *' is the error

Do Node* cur = head; or const Node* cur = &head;

This is because head is a const reference and you can't assign it to plain cur(which is NODE*).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM