C++ 错误:没有匹配的调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9682548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:10:03  来源:igfitidea点击:

error: no matching function for call to

c++

提问by Aikiman001

Heres my error...

这是我的错误...

"In constructor 'NumGame::NumGame(int&)': error: no matching function for call to 'Category::Category()'"

“在构造函数‘NumGame::NumGame(int&)’中:错误:没有匹配的函数调用‘Category::Category()’”

Ive looked at a few similar questions here but cant seem to find an answer. I have a base class Category and NumGame is inherited from it but wont compile.

我在这里查看了一些类似的问题,但似乎无法找到答案。我有一个基类 Category 和 NumGame 继承自它但不会编译。

class Category {

public:
    void virtual selection(int&);
    Category(int&);
    virtual ~Category(){};
private:
    int myRandNum;
};

Category::Category(int& a){
    myRandNum = a;
}

void Category::selection(int& a){
    cout << "I am NumGame version number... " << a << endl;
    cout << "Now Im playing... " << myRandNum << endl;
}

class NumGame : public Category {

public:
    void selection(int&);
    NumGame(int&);
    ~NumGame(){};
private:
    int myRandNum;
};

NumGame::NumGame(int& b){
    myRandNum = b;
}

void NumGame::selection(int& b) {

}

回答by Alok Save

Reason for the error:

错误原因:

When you create an instance of derived class NumGamethe Base class Categoryno argument constructor is called to create the Categorypart of the object. Your class doesn't have one and the compiler complains about it.

当您创建派生类NumGame的实例时,基类Category不调用任何参数构造函数来创建Category对象的一部分。你的班级没有,编译器抱怨它。

Why the compiler did not synthesize the default constructor?

为什么编译器没有合成默认构造函数?

Once you provide any constructor for your class the compiler does not synthesize the constructor which does not take any argument for you, You have to provide that yourself if your code usesone.

一旦你为你的类提供了任何构造函数,编译器就不会合成不为你接受任何参数的构造函数,如果你的代码使用一个,你必须自己提供。

Solutions:

解决方案:

There are two ways to avoid the error:

有两种方法可以避免错误:

Call the appropriate available constructor in Base class Catoegorysubobject through Member Initializer list. This removes the scenario where your code usesa no argument constructor.

Catoegory通过Member Initializer list在 Base 类子对象中调用适当的可用构造函数。这消除了您的代码使用无参数构造函数的情况。

NumGame::NumGame(int& b) : Category(b)
{

}

OR

或者

You need to provide a no argument constrcutor for Categoryclass yourself:

您需要Category自己为类提供无参数构造函数:

Category::Category()
{

}

回答by Mankarse

Categorydoes not have a default constructor, so you need to supply arguments when constructing the Categorybase object of NumGame:

Category没有默认构造函数,因此在构造 的Category基础对象时需要提供参数NumGame

NumGame::NumGame(int& b) :
    Category(b)
{
    myRandNum = b;
}

回答by andrea.marangoni

in your NumGameclass you have to provide a call to constructor of base class. if you don t, compiler do it for you with default contructor : in your case Category()that you don t have..

在您的NumGame类中,您必须提供对基类构造函数的调用。如果你不这样做,编译器会用默认的构造函数为你做:在你的情况下Category(),你没有..