   accessing variables in a vector of class objects
  It was suggested to me earlier that I change my code to use vectors instead of arrays.  I have since run into a conundrum.Here is the definition of my class of balls[source lang=cpp]class Ball { public:   Ball(void, void);   ~Ball();   WallCollision (int, int, int, int);    //accessor methods   void SetXVelocity (int x) { itsXVelocity = x };   void SetYVelocity (int y) { itsYVelocity = Y };   int GetXVelocity () const { return itsXVelocity; }   int GetYVelocity () const { return itsYVelocity; }    private:    Point itsPositionint itsXVelocity;    int itsYVelocity;    bool itsExistence //default false... note to self: FIGURE OUT HOW...};In main I am trying to use a for loop to check/modify the "itsExistence" value of every ball.  Here is the gist of what I have done[source lang=cpp]    Ball Ball_1, Ball_2;    vector<class> BallHolder;    vector<class>::iterator iter;    vector<class>::const iterator const_iter;    BallHolder.pushback(Ball_1);    BallHolder.pushback(Ball_2);for (iter = BallHolder.begin(); iter != Ballholder.end(); ++iter){BallHolder[iter] = so I'm trying to iterate through all the Balls in the BallHolder.  I can do that. But how can I choose to select and modify the Ball.itsExistence value?Thank you for the help.   
std::vector< Ball > Ballholder;std::vector< Ball >::iterator i, e;i = Ballholder.begin(), e = Ballholder.end();for( ; i != e; ++i )    ( *i ).itsExistence = true;This should work. The main thing is that the argument to the std::vector is the class you want to use. so std::vector< Ball > instead of std::vector< class >.Then, iterators point to an object in the vector. So, you can just use the iterator to access the element.- me   ;  There is a slight problem with inferno82's suggestion in that itsExistence is declared "private" and so you can't read/change its value from outside of the class. This can be fixed by either making that variable public or adding some functions to get/set the value like your velocity functions.   ;  class Ball  {  public:    // ...    bool exists() const { return itsExistence; } // ...  };  for (iter = BallHolder.begin(); iter != Ballholder.end(); ++iter)  {    if (iter->exists())    { // ...    }  }    ;  Another way to iterate through your vector is to do the following:std::vector&lt;Ball&gt; vBalls;for (int i=0; i&lt;(int)vBalls.size(); ++i) {    if (vBalls-&gt;exists()) {//..    }};Quote:Original post by mmakrzemAnother way to iterate through your vector is to do the following:*** Source Snippet Removed ***Looks like your formatting got messed up. Here's the corrected example:std::vector<Ball> vBalls;for (int i=0; i<(int)vBalls.size(); ++i) {    if (vBalls->exists()) {//..    }}Note that since the Ball objects are stored by value, it should be the . operator here rather than the -> operator.   ;  So I have modified the program a bit.  I think I'm doing this correctly.I modified my class ball so that there is a public accesser method that we can use to modify itsExistence.class Ball { public:    //... void SetExistence (int truth) { itsExistence = truth; } int GetExistence () const { return itsExistence; } private:    bool itsExistence;    //...};Then here is what I did with the code.  I wrote a simple snippet of code that sets all of the itsExistence values to false.for (iter = BallHolder.begin(); iter != Ballholder.end(); ++iter){BallHolder[iter->SetExistence(false)];}and I think that last line can alternatively be written:BallHolder[iter->SetExistence(false)]am I right?   ;  And while were on the topic of vectors... Here is another question.I keep getting some syntax errors when trying to compile my point class.The compiler keeps telling me two errors numerous times, clearly because I'm violating the same rule on each line: 1. syntax error : identifier 'vector'2. 'Ball' : illegal use of this type as an expressionI looked up how to use vector types in function definitions, and the site confirmed that I was using the correct syntax.  I reasoned that the Ball errors might be because the correct header files were not linked correctly, so i tried linking the Ball.h to no avail... I also included <vector> just to see if that would help some of the other errors.Here is class:I just realized what one issue might be...  In the Distance function, I only want to pass in a single object from the Ball vector, not the entire vector...Is there any resource out there that gives a ton of examples explaining proper syntax for different situations?[source lang=cpp]#pragma once#include "Ball.h"#include <vector>class Point { public:   Point (int x =0, int y = 0);   ~Point (); //accessor methods int Distance (vector < Ball > Ball_1, vector < Ball > Ball_2); //player ball and other ball can collide and two other balls can collide int Distance (vector < Ball > Ball_1, class PlayerBall Ball_2);   void SetX ( int x ) { itsX = x; }   void SetY ( int y ) { itsY = y; }   int GetX() const { return itsX; }   int GetY() const { return itsY; }    private:    int itsX;    int itsY;};;  nope. read more carefully what was stated above. (check Bregma's post)You dont need the BallHolder[] stuff at all.iter->SetExistence(false);is the entirety of the that line of code to set the existance.And you need to call it "std::vector" for it to compile. (or much less recomended, but you can also put a using std::vector or using namespace std after the #includes. Problem is that it "pollutes" the namespace of anything including your header, and that can cause problems.)   ;Quote:Original post by KulSerannope. read more carefully what was stated above.You dont need the BallHolder[] stuff at all.iter->SetExistence(false);is the entirety of the that line of code to set the existance.and you need to call it "std::vector" for it to compileooo. I get it.  Iter is already pointing to the address of object the vector.  That makes perfect sense.   