Access pointer methods of pointer vector

Jul 12, 2018 at 2:50am
I'm not really sure how to phrase the question exactly correctly, but I've declared two vectors
1
2
3
	
std::vector<enemy*> enemies;
std::vector<hit_area *> effects;

these work and are fine, however I pass them to a function with
 
handleGame(strd_maps[0], &gcam, &mainchar, currentKeyStates, &enemies, &effects);

which works and is fine, however in the function, when I try to access members or methods or enemies or effect
if(effects[d]->collide(enemies[i]->x, enemies[i]->y enemies[i]->w, enemies[i]->h))
I get the error "base operand of "->" has non-pointer type 'std::vector<enemy*>*'.
I can access the size of both enemies and effects, it's just accessing the methods that is giving problems
Last edited on Jul 12, 2018 at 2:50am
Jul 12, 2018 at 3:21am
Normally you don't pass the address of a vector. Instead, you pass a reference:

1
2
3
4
5
6
7
void func(std::vector<enemy*> &enemies) {
    std::cout << enemies.size() << '\n';
    std::cout << enemies[i].x << '\n';
}

// Call it like:
func(enemies);


If you pass the address of the vector, then you need to use some slightly wacky syntax, like:

 
std::cout << enemies->operator[](i).x << '\n';

So use a reference.
Last edited on Jul 12, 2018 at 3:22am
Jul 12, 2018 at 3:37am
Thanks heaps, worked perfectly
Jul 12, 2018 at 5:13am
I'm kind of curious why you're using a vector of pointers to begin with.
Jul 12, 2018 at 8:15pm
repackage data? Eg, say you had a list or tree or something of data and you don't want to mess with its order or structure, but you need to print a sorted list of it. Vector of pointers to all the nodes will solve this with minimal copying and pain, even easier if you maintain it alongside the other structure. Contrived example, I guess? But there are times when its handy to make an array out of something else.


Topic archived. No new replies allowed.