Noxilus
Noxilus

Reputation: 35

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!

Upvotes: 0

Views: 301

Answers (2)

v010dya
v010dya

Reputation: 5858

The sequence of what is currently happening:

  1. (somewhere in main) Attempt to create an instance of Character.
  2. Character is creating all of its data members: inventory and foodInv.
  3. Since inventory is an array the constructor needs to be called 30 times, and it needs to be called before the execution of the constructor of Character. Because you don't provide the compiler with anything to call specifically, it will attempt to call the default constructor. And since you have defined parameterised constructor, the default has been deleted.

What you need:

Option1

in your header file:

class Character {
    InvSlot inventory[30] = {emptyInv,...,emptyInv};
    FoodSlot foodInv[10] = {emptyFood,...,emptyFood};

This is probably not the best option, since you clutter the header.

Option2

In the definition of your constructor:

Character::Character()
  : inventory{{emptyInv,...,emptyInv}}, foodinv{{emptyFood,...,emptyFood}}
{
}

Funny thing is that you already have the : symbol in your constructor definition, meaning that you have first attempted the correct solution (or perhaps you were unaware of the meaning of that character). It is there specifically to call constructors on the fields.

Upvotes: 0

Alexander Stante
Alexander Stante

Reputation: 69

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).

Upvotes: 0

Related Questions