Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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++程序时: menutype::menutype(int cat_num){ extras list = extras(cat_num); } extras::extras(int num_cats){ head = new category_node; head->next = NULL; head->category = 1; category_node * temp; for(int i = 1; i < (num_cats); ++i){ temp = new category_node; temp->next = head->next; head->next = temp; temp->category = (num_cats-(i-1)); } } menutype::menutype(int cat_num){ 附加列表=附加(类别编号); } 额外服务::额外服务(int num_猫){ head=新类别\节点; head->next=NULL; 头部->类别=1; 类别节点*temp; 对于(int i=1;i下一步=头部->下一步; 头部->下一步=温度; 临时->类别=(数量(i-1)); } }_C++_Class_Object_Constructor - Fatal编程技术网

C++;构造函数调用另一个构造函数 当试图构建一个包含以下代码的C++程序时: menutype::menutype(int cat_num){ extras list = extras(cat_num); } extras::extras(int num_cats){ head = new category_node; head->next = NULL; head->category = 1; category_node * temp; for(int i = 1; i < (num_cats); ++i){ temp = new category_node; temp->next = head->next; head->next = temp; temp->category = (num_cats-(i-1)); } } menutype::menutype(int cat_num){ 附加列表=附加(类别编号); } 额外服务::额外服务(int num_猫){ head=新类别\节点; head->next=NULL; 头部->类别=1; 类别节点*temp; 对于(int i=1;i下一步=头部->下一步; 头部->下一步=温度; 临时->类别=(数量(i-1)); } }

C++;构造函数调用另一个构造函数 当试图构建一个包含以下代码的C++程序时: menutype::menutype(int cat_num){ extras list = extras(cat_num); } extras::extras(int num_cats){ head = new category_node; head->next = NULL; head->category = 1; category_node * temp; for(int i = 1; i < (num_cats); ++i){ temp = new category_node; temp->next = head->next; head->next = temp; temp->category = (num_cats-(i-1)); } } menutype::menutype(int cat_num){ 附加列表=附加(类别编号); } 额外服务::额外服务(int num_猫){ head=新类别\节点; head->next=NULL; 头部->类别=1; 类别节点*temp; 对于(int i=1;i下一步=头部->下一步; 头部->下一步=温度; 临时->类别=(数量(i-1)); } },c++,class,object,constructor,C++,Class,Object,Constructor,我收到错误消息: cs163hw1.cpp:在构造函数“menutype::menutype(int)”中: cs163hw1.cpp:59:31:错误:调用“extras::extras()”时没有匹配的函数 cs163hw1.cpp:59:31:注:候选人为: cs163hw1.cpp:5:1:注:附加::附加(int) 我不明白为什么,请帮忙 由于该行不应尝试调用默认构造函数(仅从int复制构造函数和转换构造函数),我猜您的类menutype中有一个类型为extras的数据成员,因此,您必

我收到错误消息:

cs163hw1.cpp:在构造函数“menutype::menutype(int)”中:
cs163hw1.cpp:59:31:错误:调用“extras::extras()”时没有匹配的函数
cs163hw1.cpp:59:31:注:候选人为:
cs163hw1.cpp:5:1:注:附加::附加(int)


我不明白为什么,请帮忙

由于该行不应尝试调用默认构造函数(仅从
int
复制构造函数和转换构造函数),我猜您的类
menutype
中有一个类型为
extras
的数据成员,因此,您必须在初始值设定项列表中对其进行初始化,因为它没有默认构造函数:

menutype::menutype(int cat_num) : list(cat_num) { //or whatever the member is called

}

由于该行不应尝试调用默认构造函数(仅从
int
复制构造函数和转换构造函数),我猜您的类
menutype
中有一个类型为
extras
的数据成员,因此,您必须在初始值设定项列表中对其进行初始化,因为它没有默认构造函数:

menutype::menutype(int cat_num) : list(cat_num) { //or whatever the member is called

}

您的
菜单类型
似乎包含类型为
extras
的成员。如果是这种情况,并且如果
extras
没有默认构造函数(似乎是这样),则需要在初始化列表中对其进行初始化:

menutype::menutype(int cat_num) : myextrasmember(cat_num) {}

您的
菜单类型
似乎包含类型为
extras
的成员。如果是这种情况,并且如果
extras
没有默认构造函数(似乎是这样),则需要在初始化列表中对其进行初始化:

menutype::menutype(int cat_num) : myextrasmember(cat_num) {}

通常,可以通过以下方式调用另一个类(如您的示例中)的构造函数中的构造函数:

menutype::menutype(int cat_num)
:列表(类别编号)
{
}


当在初始化器列表中调用list的构造函数(类型为extra)时,这会更有效。

通常会通过以下方式调用另一个类的构造函数中的构造函数(如您的示例所示):

menutype::menutype(int cat_num)
:列表(类别编号)
{
}


由于在初始化器列表中调用了list(类型为extra)的构造函数,这会更有效。

正如其他人所说,您调用的构造函数不正确

另外三个人已经指出了正确的初始值设定项列表方法。然而,没有人指出如何在构造函数上下文之外正确调用构造函数

而不是:

extras list = extras(cat_num);
做:


正如其他人所说,您错误地调用了构造函数

另外三个人已经指出了正确的初始值设定项列表方法。然而,没有人指出如何在构造函数上下文之外正确调用构造函数

而不是:

extras list = extras(cat_num);
做: