0

So, I have a hero structure that contains name, health, and attack. I'm allowing the user to enter how many heroes they want to create and create an array of that many heroes (I also had problems allowing the user to determine the array size, so the problem could be there). When trying to set the attributes using a loop of the array, I get an error: IntelliSense: no operator "[]" matches these operands. operand types are: Hero [ int ]

My question is, how do I loop through an array of a structures to set their attributes and if so, would displaying the hero's information be similar with a display function?

struct Hero
{
private: 
    string hName;
    int hHealth;
    int hAttack;

public:
    void displayHeroData();
    void setName(string);
    void setHealth(int);
    void setAttack(int);
};

void Hero::displayHeroData()
{
cout << "\n\n\n\nHERO INFO:\n" << endl;
cout << "Name: " << hName << endl;
cout << "Health: " << hHealth << endl;
cout << "Attack: " << hAttack << endl;
};

void Hero::setName(string name)
{
hName = name;
}

void Hero::setHealth(int health)
{
if(health > 0)
    hHealth = health;
else
    hHealth = 100;
}

void Hero::setAttack(int attack)
{
if(attack > 0)
    hAttack = attack;
else
    hAttack = 100;
}

int main()
{
string name;
int health;
int attack;
int num;
Hero *heroList; //declaring array

      //getting size of array
cout << "How many hero's do you want to create? (greater than 0)" <<endl;
cin >> num;
heroList = new Hero[num]; //this is the array of Heroes

      //looping through the array
for(int x = 0; x < num; ++x){

      //creating a new hero, I think???
Hero heroList[x];

      //setting hero's name
cout << "What is hero" << x <<"'s name?" << endl;
cin >> name;
heroList[x].setName(name);

      //display the character after attributes have been set
heroList[x].displayCharacterData();

      }//end of for loop


return 0;
}

3 Answers 3

2
Hero heroList[x];

Remove this line for good. no need for it.

Sign up to request clarification or add additional context in comments.

3 Comments

How did it work? You did reinitialize the array (heroList) with a size which is not constant.
The answer says that this line should be removed.
@hasan, the line must be removed to be more precise otherwise even if his compiler allows that (as he implied), he will get a scope's problem because he reinitialized the array inside the loop.
1

The only problem that you will encounter after removing the line proposed (Hero heroList[x]) is that all the hero objects created aren't initialized (when you called new Hero[num] you only allocated the array and created the default implicit constructor for each one).

In order to init all of them, you must either use all your 'setters', or write a non-default constructor and then only allocate an array of Hero* ptrs, and when looping through it init each ptr to a new Hero(.....) with the parameters you wish.

Hope that helps clarify things.

Comments

1

In the loop just do the following

  //looping through the array
    for(int x = 0; x < num; ++x)
    {
        cout << "What is hero" << x <<"'s name?" << endl;
        cin >> name;
        heroList[x].setName(name);
    }

    for(int x = 0; x < num; ++x)
    {
        heroList[x].displayHeroData();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.