Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

上个月关闭。



Improve this question




我正在做一些列表练习,但是由于某种原因,编译器注意到有关struct节点类型的一些问题,但是我看不到问题出在哪里。
我声明了正常的节点结构,在addNode(node *&, int)函数中,我使用了
通过node类型引用的指针来修改主列表,并保存一个整数以保存在节点中
#include<iostream>

struct node
{
    int data;
    node *next;
};

void addNode (node *& ,int);

int main()
{
    node list;

    addNode(list, 24);

    system("pause");
    return 0;
};

但是当我尝试在主函数中使用该函数并进行编译时,这会出现在编译器中:
exercise1.cpp: In function 'int main()':
exercise1.cpp:19:10: error: invalid initialization of reference of type 'node*&' from expression of type 'node'
   19 |  addNode(list, 24);
      |          ^~~~
exercise1.cpp:9:15: note: in passing argument 1 of 'void addNode(node*&, int)'
    9 | void addNode (node *& ,int);
      |               ^~~~~~~
如果我按引用放置node*&仍然发生相同的错误,则在其他练习和工作中使用相同的语法之前,node其引用类型为list的指针将主要修改&list发生了什么?

最佳答案

int main()
{
     node list;
缺少星星来声明指针。您可能想要:
int main()
{
     node* list = NULL;

关于c++ - C++-从 'node*&'类型的表达式对 'node'类型的引用的无效初始化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63649260/

10-11 23:05
查看更多