adding to a vector from within a class?

I made a Particle class and i made a vector of that class in my “testApp”. Now i have another class called “Creature” wich will create new Particles. How can i add to that vector from within the Creature class? Can i make a pointer to a vector and use that as a reference??

in my testApp header file i put:
vector particles;

and i want to make something like this from within my Creatures class:

Criatura::Criatura(Blob *_blob){  
	blob = _blob;  
	angOffSet = 0;  
	comprimento = 35;  
	particles.push_back(Particle(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight())));  

im getting “error: ‘Particle’ was not declared in this scope”

p.s.: (“Criatura” means “Creature” in portuguese…)

any help would be much apretiated.


i actually got it to work… i had a vector like this

vector <Particles> particles  

and when i initiated my “Criatura” object i passed in the parameter “&particles”
and my Criatura constructor looks like this:

Criatura::Criatura(Blob *_blob, vector<Particles> *_p){  
	blob = _blob;  
	angOffSet = 0;  
	comprimento = 35;  
	p->push_back(Particles(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight())));  

hope its not too confusing…

re vectors + pointers, Todd, remember that big where you had to say something like:


I’m still re-familiarizing myself with C++, and just noticed something in the vector documentation. I think the more standard way to get an element in a vector via a pointer to the vector is like this:


Because at() and [] act similarly, but [] is an operator rather than a method.

yea the at() also checks the bounds. So if you pass it an index outside of the bounds of the vector it throws an exception (a deliberate crash, so you know exactly what happened and when) - as opposed to it running without warning that you’ve tried to access an index out of bounds and the app not working properly and it crashed later on some other line and you go crazy trying to figure it out.