Search code examples
c++functionconstructorcandidate

Constructor expecting missing call to other constructor / candidate expects 1 argument, 0 provided?


You may have seen my post yesterday about the issues I had been having with my text-based adventure game. I got all of that fixed up and added some functionality, but in adding some inventory arrays of custom Slot objects my Character object decided to stop recognizing their initialization.

Here's the basic code:

Character Class

class Character {
    InvSlot inventory[30];
    FoodSlot foodInv[10];
    //Declaring armor slots, class, race, stats, etc

public:
    //All of the function declarations

Character Constructor

Character::Character() :
    //Initializing armor slots, class, race, stats, etc to default values
{
    InvSlot inventory[30] = {emptyInv,...,emptyInv}; //The other 28 values are omitted here.
    FoodSlot foodInv[10] = {emptyFood,...,emptyFood}; //The other 8 values are omitted here.
}

InvSlot and FoodSlot Structs w/ Basic Empty Slots

struct InvSlot {
    bool isUsed;
    Item item;
    InvSlot(Item i);
};

struct FoodSlot {
    bool isUsed;
    Item item;
    FoodSlot(Food f);
};

InvSlot::InvSlot(Item i) : isUsed(false), item(i){}
InvSlot emptyInv = InvSlot(NoItem); //NoItem is a basic object of type Item which works

FoodSlot::FoodSlot(Food f) : isUsed(false, food(f), quantity(0){}
FoodSlot emptyFood = FoodSlot(NoFood); //Same deal with NoFood

The Errors

First of all, the struct declarations and constructor definitions for InvSlot and FoodSlot are giving me the issue of candidate expects 1 argument, 0 provided. Second of all, the actual errors are on the final initializer in the Character constructor (before the curly braces): no matching function for call to 'FoodSlot::FoodSlot()' and no matching function for call to 'InvSlot::InvSlot()'. The thing is, I have already initialized the arrays in the curly braces and they aren't returning any errors themselves. On top of this, these two are the only two parameters for the character class which even use the InvSlot or FoodSlot objects. Can anybody help figure out what's going on, and how to fix it? I already checked other similar questions but they didn't help. Let me know if I need to add anything or if I missed something when typing up this post.

Thanks in advance!


Solution

  • The error message you get is basically that the default constructor can't be found, which is obviously the case. When you write

    InvSlot inventory[30];
    FoodSlot foodInv[10];
    

    the default constructor is called to initialize your arrays. What you can do is to write a default constructor for InvSlot and FoodSlot which basically return an empty InvSlot and FoodSlot. With empty I mean the semantically equivalent of the objects you used in your initializer list (emptyInv, emptyFood).