error: no matching function for call to

前端 未结 3 1340
萌比男神i
萌比男神i 2021-01-03 01:52

Heres my error...

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

Ive looked at a few sim

相关标签:
3条回答
  • 2021-01-03 01:57

    Category does not have a default constructor, so you need to supply arguments when constructing the Category base object of NumGame:

    NumGame::NumGame(int& b) :
        Category(b)
    {
        myRandNum = b;
    }
    
    0 讨论(0)
  • 2021-01-03 02:01

    Reason for the error:

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

    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 uses one.

    Solutions:

    There are two ways to avoid the error:

    Call the appropriate available constructor in Base class Catoegory subobject through Member Initializer list. This removes the scenario where your code uses a no argument constructor.

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

    OR

    You need to provide a no argument constrcutor for Category class yourself:

    Category::Category()
    {
    
    }
    
    0 讨论(0)
  • 2021-01-03 02:13

    in your NumGame class 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..

    0 讨论(0)
提交回复
热议问题