Using functions in a vector of classes.

closed account (2NywAqkS)
I have a vector of classes for bullets but I'm not quite sure how to use the functions the class when its in a vector.

To initialise:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Bullet
{
	bool Friendly;
private:
	double x,y,prevx,prevy,speed,direction;
public:
	void Draw_Bullet ();
	void Move_Bullet ();
	Bullet(double,double,double);
};

Bullet::Bullet(double ax, double ay, double adirection)
{
	x = ax;
	y = ay;
	direction = adirection;
}

void Bullet::Draw_Bullet ()
{
	glBegin(GL_LINES);
	glColor4f(1,1,1,1);
      glVertex2f(x,y);
	glColor4f(1,1,0.9,0);
	  glVertex2f(prevy,prevy);
	glEnd();
}

void Bullet::Move_Bullet ()
{
	x += cos(direction) * speed;
	y += sin(direction) * speed;
}

vector<Bullet> Player_Bullets_Vector;


When the player shoots.

 
Player_Bullets_Vector.push_back(Bullet(Xpos,Ypos,Soldier_radian));


and in the loop I tried:

1
2
3
4
5
for (int i = 0; i <= Player_Bullets_Vector.size; i++)
	{
		Player_Bullets_Vector.at(i).Draw_Bullet;
		Player_Bullets_Vector.at(i).Move_Bullet;
	}


However this has comeback with a load of errors can anyone see any problems?

Thanks Rowan
Here's two issues with this line:
for (int i = 0; i <= Player_Bullets_Vector.size; i++)

1. It's .size() not .size;
2. When i == Player_Bullets_Vector.size() you're trying to access an element that doesn't exist. You'll get a runtime error.

Here's a safer way to do this sort of thing: using iterators:
1
2
3
4
5
6
vector<Bullet>::iterator it; // Make an iterator
for (it = Player_Bullets_Vector.begin(); it < Player_Bullets_Vector.end(); ++it)
{
  it->Draw_Bullet(); // it is a pointer to the current object, access members with ->
  it->Move_Bullet(); // You have to use () when calling functions, even if there are no arguments
}
Another way is to use for based on range

1
2
3
4
5
for ( auto & i : Player_Bullets_Vector )
{
   i.Draw_Bullet();
   i.Move_Bullet();
}
Is that C++11? It doesn't compile in VS2010.
Yes indeed MS VC++ 2010 does not support for based on range. But some other compilers support already this feature.
>and in the loop I tried:
1
2
3
4
5
for (int i = 0; i <= Player_Bullets_Vector.size; i++)
	{
		Player_Bullets_Vector.at(i).Draw_Bullet;
		Player_Bullets_Vector.at(i).Move_Bullet;
	}

> However this has comeback with a load of errors

The size() member function of a vector gives the number of elements in the vector; if the size is N, like in an array the valid positions are 0, 1, 2, ..., (N-1)

If the position is a valid position, we can just use the subscript operator [] on the vector.

Like size() in a vector, Draw_Bullet() and Move_Bullet() are member functions of Bullet

1
2
3
4
5
6
7
8
// for (int i = 0; i <= Player_Bullets_Vector.size; i++)
for( int i = 0 ; i < Player_Bullets_Vector.size() ; ++i )
{
    //Player_Bullets_Vector.at(i).Draw_Bullet;           
    Player_Bullets_Vector[i].Draw_Bullet() ;
    //Player_Bullets_Vector.at(i).Move_Bullet;
    Player_Bullets_Vector[i].Move_Bullet() ;
}


Last edited on
Topic archived. No new replies allowed.