-3

I'm new to C++ and I'm trying to dry up my code, for example:

void gethit () {
    Gert.hitbox(AA.x, AA.damage);
    Gert.hitbox(AB.x, AB.damage);
    Gert.hitbox(AC.x, AC.damage);
    Gert.hitbox(AD.x, AD.damage);
    Gert.hitbox(Terbil.x, Terbil.damage);
}

AA, AB, AC, AD and Terbil are all instances of a class called Entity with variables x and damage.

Every time I want to add a new instance I'll have to come into this function and add it manually. I'm trying to add all of the address of the instances to an array like so:

void * p_enemys[10];

p_enemys[0] = &AA;
p_enemys[1] = &AB;
p_enemys[2] = ∾
p_enemys[3] = &AD;
p_enemys[4] = &Terbil;

Just wondering how I could call a function from the instance via the array, I tried to do

for(int i = 0; i < 10; i++;) {
    Gert.hitbox(p_enemys[i].x, p_enemys[i].damage);
}

and g++ compiler spits out: "request for member `damage' in `p_enemys[i]', which is of non-aggregate type `void *'"

I don't really need to use arrays specifically any help is very appreciated.

Changes made, thanks @gldraphael!

vector <Entity*> p_Enemys(10);

void gethit () {
    for (int i = 0; i < 10; ++i) {
    Entity * ienemy = (Entity*) p_Enemys[i];
        Gert.hitbox((ienemy->x), (ienemy->damage));
    }
}

1 Answers1

1

You can make an a std::vector as follows:

std::vector <Entity*> p_Enemys(10);

The assigning part remains the same:

p_enemys[0] = &AA;
p_enemys[1] = &AB;
p_enemys[2] = &AC;
p_enemys[3] = &AD;
p_enemys[4] = &Terbil;

You can then loop through the p_enemys as follows:

for(auto i : p_enemys) {
    Gert.hitbox(i->x, i->damage);
}

So what was it that you missed?
The array was declared as an array of void* So, in the loop, p_enemys[i] returned a void*.

Also class/struct members are accessed using a dereferencing operator ->. You used the membership operator . instead.

So this code should have worked instead:

for(int i = 0; i < p_enemys.size(); i++;) {     // 
    Entity * ienemy = (Entity*) p_enemys[i];    // cast the void* into a Entity*
    Gert.hitbox(ienemy->x, ienemy->damage);
}


As a general rule, avoid void*s whenever possible.
galdin
  • 12,411
  • 7
  • 56
  • 71
  • changed void* p_Enemys[10]; to vector p_Enemys[10]; now it says: s Quest\Terbil quest basic.cpp|149|error: 'class std::vector' has no member named 'x'| I'm using your edited version of my for loop. – Garrett Hale Oct 05 '14 at 22:24
  • @GarrettHale which line are you getting that error for? – galdin Oct 05 '14 at 22:27
  • @GarrettHale could you share the `Entity` struct/class's definition? – galdin Oct 05 '14 at 22:30
  • Gert.hitbox(((Entity*)p_Enemys->x),((Entity*)p_Enemys->damage)); – Garrett Hale Oct 05 '14 at 22:30
  • Hey @GarrettHale you've placed the bracket in the wrong place. Please check. – galdin Oct 05 '14 at 22:38
  • sorry this is the error with brackets, I was just trying everything: s Quest\Terbil quest basic.cpp|149|error: base operand of '->' has non-pointer type 'std::vector'| it's for the same line – Garrett Hale Oct 05 '14 at 22:42
  • @GarrettHale I'd highly appreciate if you could post the changes you've made to the code as an update to the question (keeping the current code) – galdin Oct 05 '14 at 22:45
  • In this code the `for` loop dereferences null pointers; you need to get the vector size in sync with the code for iterating over it – M.M Oct 05 '14 at 22:45