C++构造函数调用另一个构造函数

Fle*_*515 1 c++ constructor class object

尝试构建包含以下代码的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));
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

cs163hw1.cpp:在构造函数'menutype :: menutype(int)'中:
cs163hw1.cpp:59:31:错误:没有匹配函数来调用'extras :: extras()'
cs163hw1.cpp:59:31:注意:候选人是:
cs163hw1.cpp:5:1:注意:extras :: extras(int)

我不明白为什么,请帮忙!

Luc*_*ore 5

由于该行不应该试图调用默认的构造函数(只拷贝构造函数和转换构造函数int),我就猜你有类型的数据成员extras在类中menutype,所以你必须在初始化列表中,因为对其进行初始化它没有默认构造函数:

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

}
Run Code Online (Sandbox Code Playgroud)