0
\$\begingroup\$

I created a class in a separate .h and .cpp file with the idea of having an array of 3 monsters with each monster having different stats( name; health; damage). The problem is that the constructor should change the properties but when I try to call them in the main file only the 3rd monster's properties are changed.

main.cpp

#include <string>
#include <iostream>
#include <Windows.h>
#include <stdio.h>
using namespace std;



#include "Player.h"
#include "Monsters.h"
#include "MonsterCluster.h"
int main()
{

    Player player;
    Monsters monster;
    MonsterCluster enemies;

    enemies = MonsterCluster(0, "Goblin", 20, 5);
    enemies = MonsterCluster(1, "Wraith", 50, 10);
    enemies = MonsterCluster(2, "Legionare", 80, rand() % 60+20);

    cout << enemies.monsterList[2].name << endl;
}

MonsterCluster.h

#pragma once
#include "Monsters.h"

class MonsterCluster :
    public Monsters
{
public:
    Monsters monsterList[3];



    MonsterCluster();
    MonsterCluster(int _monsterIndex, string _name, int _health, int _damage);
   
    
}

MonsterCluster.cpp

MonsterCluster::MonsterCluster()
{
    /*name = "";
    health = 2;
    damage = 3;*/
}

MonsterCluster::MonsterCluster(int _monsterIndex, string _name, int _health, int _damage)
{
    monsterList[_monsterIndex].name = _name;
    monsterList[_monsterIndex].health = _health;
    monsterList[_monsterIndex].damage = _damage;
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Don't write using namespace std;.

You are passing std::string by value, which copies the string. Generally you want to pass as const std::string&, or better yet std::string_view. (Though in the case of a properly written constructor (see below) you might want to do something more advanced here.)


enemies = MonsterCluster(0, "Goblin", 20, 5);
enemies = MonsterCluster(1, "Wraith", 50, 10);
enemies = MonsterCluster(2, "Legionare", 80, rand() % 60+20);

You are assigning values to the same variable, overwriting the previous.

I think you're confusing the meaning of monsterList inside the class with uses of the entire class. Each instance you create is populating a different element in the array, but you don't understand that these calls are creating different instances and not updating the previous instance! You throw away the first two instances you create and only keep the third.

Have a Monster class with a constructor. You don't show your Monsters but it appears to be a plain struct without a constructor, from how you're using it.

Then you don't need a class that's nothing but a collection of 3 Monsters. That's just an array.

Something like:

Monster enemies[] = {
   { "Goblin", 20, 5 },
   { "Wraith", 50, 10 },
   { "Legionare", 80, rand() % 60+20 }
};

Note that each line is three parameters for the Monster constructor.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you are going to edit and answer, do the edit first so that it is not flagged as a possible answer invalidation. \$\endgroup\$ Commented May 13, 2021 at 14:37
  • \$\begingroup\$ I didn't notice the formatting issue until I had already posted. Anyone who reviews it should see it just adds backquotes around the code block. \$\endgroup\$ Commented May 13, 2021 at 14:40
0
\$\begingroup\$

Additionally to JDługosz's answer:

  • Don't include headers (<stdio.h> and <Windows.h>) that you don't use. It wastes the compiler's time and energy, and in the case of the latter, makes the program needlessly platform-dependent.
  • Don't write headers that have requirements on what must be declared before them (e.g. std::string and using std::string)
  • Don't begin identifiers with _ (even if you are one of the few who understand exactly when that is allowed - you'll just mislead future maintainers).
  • Don't declare a default constructor if you don't need one.
  • Do include <cstdlib> before using std::rand().
  • Don't flush output with std::endl where a plain non-flushing newline ('\n') will do.
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.