Home > Blockchain >  Can I add a map as an attribute of a class in C ?
Can I add a map as an attribute of a class in C ?

Time:06-15

I'm creating a program with C that rates Elden Ring bosses. I'd like to add a map as an attribute of my parent boss class. Here's what I'm trying:

class Boss {
public:
    string name;
    string type;
    int hp;
    bool parriable;
    bool breakPoise;
    string damageTypeDealt;
    map< string, int > absorptions;
    map< string, int > resistances;}

When I attempt to create an instance of that class:

// BOSS OBJECTS
Boss godrickTheGrafted(
    "Godrick the Grafted", 
    "Legend Boss", 
    6080, 
    false, 
    true,
    "Standard Damage, Fire Damage",
    absorptions.insert(pair<string, int>("Phy (Standard)", 0)),
    resistances.insert(pair<string, int>("Not yet", 0))
    );

I get an error under each map insert saying "identifier not defined". I'm declaring the class before my main class currently. Can I even do this? Thanks for any help.

CodePudding user response:

You can have maps just like strings or any other container/structure from standard library. Your call to the constructor is not correct, though:

Boss godrickTheGrafted
(
    "Godrick the Grafted", 
    "Legend Boss", 
    6080, 
    false, 
    true,
    "Standard Damage, Fire Damage",
    absorptions.insert(pair<string, int>("Phy (Standard)", 0)), // <-- !!!
    resistances.insert(pair<string, int>("Not yet", 0))         // <-- !!!

);

You cannot call insert on a member (that actually at this point even doesn't exist yet) to create a function parameter to fill data with.

You can call insert afterwards:

Boss godrickTheGrafted
(
    "Godrick the Grafted", 
    "Legend Boss", 
    6080, 
    false, 
    true,
    "Standard Damage, Fire Damage"
);

godrickTheGrafted.absorptions.insert(pair<string, int>("Phy (Standard)", 0));
godrickTheGrafted.resistances.insert(pair<string, int>("Not yet", 0));

provided the maps are public as in your example.

Alternatively you can have maps in the constructor you can create them on the fly as:

Boss godrickTheGrafted
(
    "Godrick the Grafted", 
    "Legend Boss", 
    6080, 
    false, 
    true,
    "Standard Damage, Fire Damage",
    { pair<string, int>("Phy (Standard)", 0) },
    { pair<string, int>("Not yet", 0) }

    // or simpler just
    //{ { "Phy (Standard)", 0 } },
    //{ { "Not yet", 0 } }
);

if your constructor contains two parameters of type std::map.

CodePudding user response:

Yes you can have a data member of type std::map in a class. But the syntax you're using to create an object of Boss is wrong in that you're trying to use insert on the parameter.

You can solve this as follows.

int main()
{
     // BOSS OBJECT
     Boss godrickTheGrafted("Godrick the Grafted", "Legend Boss", 
    6080, 
    false, 
    true,
    "Standard Damage, Fire Damage",
//--vv-------------------vv-------------->note the added braces
    {{"Phy (Standard)", 0}},
    {{"Not yet", 0}});
     
}

Working Demo

  • Related