Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试调用函数_C++_Struct_Linked List - Fatal编程技术网

C++ 尝试调用函数

C++ 尝试调用函数,c++,struct,linked-list,C++,Struct,Linked List,所以我试图调用这个函数,但对动态数据结构和链表不太熟悉,所以我一直出错。这是我目前的代码: struct Country { string name; double population; }; struct Node { Country ctry; Node * next; }; Node * world; void push(Country data, Node * & list); int main () { Country data;

所以我试图调用这个函数,但对动态数据结构和链表不太熟悉,所以我一直出错。这是我目前的代码:

struct Country
 {
  string  name;
  double  population;
 };
struct Node 
 {
  Country ctry;
  Node *  next;
 };
Node * world;

void push(Country data, Node * & list);

int main ()
{
    Country data;
    Node list;

    push(data, list);
    return 0;
}

我做错了什么?

推送函数接受一个节点*&,这是指向节点指针的引用。总是向后读取类型,这很有帮助。您正在给它一个列表,它是一个节点。该函数从该调用中获取引用,但您说过要提供指向节点指针的引用,因此您希望将主列表设置为节点*,并为其分配和初始化内存,或者您希望创建另一个变量,即节点*,并将其指向列表节点*ptr=&list;。在大多数情况下,第一个选项更可取。

谢谢大家的评论,我把代码改成了这个,现在可以使用了

struct Country
 {
  string  name;
  double  population;
 };
struct Node 
 {
  Country ctry;
  Node *  next;
 };
Node * world;

void push(Country data, Node * & world);

int main ()
{
    Country data;

    push(data, world);
    return 0;
}

push的定义在哪里?push引用第二个参数的指针。您正在传递一个对象,而不是指向指针的引用。推送数据,&list以将其转换为指针。@迈克:这也不行,因为&list是一个临时指针,您不能将临时指针绑定到非常量引用。因此,要么从参数中删除引用,要么声明指向列表变量的Node*变量,然后将Node*变量传递给参数。