【发布时间】:2019-11-07 03:08:04
【问题描述】:
这里有什么问题,&head 应该产生一个指向节点的指针,这是 cur 的类型,但显然存在类型不匹配?
我正在尝试使用其头部打印链表的元素
void printList(const Node& head){
Node* cur = &head;
cout<<"[ ";
while(!(cur==0)){
cout<<cur->x<<" ";
cur = cur->next;
}
cout<<"]"<<endl;
}
无法使用“const Node *”类型的右值初始化“Node *”类型的变量是错误
【问题讨论】:
-
Head 是常量引用。您不能将其分配给非 const 变量。
-
将
cur的类型改为const Node*。