Sign in to follow this  
MeritGamer

Vectors and Error

Recommended Posts

ok, so some of you may have seen my around the forums alot latly, and if you have you will understand that I am trying to create a vector that houses draw update and dead functions for a bullet with little knowlege of how they work. However, I have come up with something that seems to make sense. vector <Bullet*> BulletVect; #define MAX_BULL 100 // The error is here: 7 C:\Users\Family\Documents\FinalProject\Include\Headers\globals.h expected constructor, destructor, or type conversion before '<' token 7 C:\Users\Family\Documents\FinalProject\Include\Headers\globals.h expected `,' or `;' before '<' token for (int index = 0; index < MAX_BULL; index ++) { Bullet *bullet; bullet = new Bullet; BulletVect.push_back(bullet); } Now, this here in my head is saying that index will always = 100 (MAX_BULL), and then will always have 100 bullets stored in the vector. Bullet is my class that houses my update draw and dead functions. What I am unclear of is that, how do I draw out a bullet from my vector? Assuming that all my functions do what they should, would using the BulletVect.pop_back(bullet); do just that for me? Also, I realize that pop_back uses the last element in the vector. I know it doesn't really matter but for understanding's sake, is there a function to use the first element? Edit: I just realized i think theres something wrong with my logic. This might be better: int index = 0; if (index != 100) { Bullet *bullet; bullet = new Bullet; BulletVect.push_back(bullet); index + 1; } And if i am correct i believe that removes the need for MAX_BULL

Share this post


Link to post
Share on other sites
i believe pop_back removes the last bullet from the back though, if you want the bullet to remain in the vector after it's drawn, and you want to draw all the bullets in the list, try:


for(int i = 0;i< BulletVec.size();i++)
{
BulletVec[i].draw();
}

or if you want to make sure you only draw "live" bullets, you can add an isDead() method to your bullet class, and liek so


for(int i = 0;i< BulletVec.size();i++)
{
if(BulletVec[i].isDead == false)
{
BulletVec[i].draw();
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by MeritGamer
vector <Bullet*> BulletVect;

#define MAX_BULL 100 // The error is here:
7 C:\Users\Family\Documents\FinalProject\Include\Headers\globals.h expected constructor, destructor, or type conversion before '<' token

Are you sure the error is at the #define and not at the BulletVect definition?
Quote:

for (int index = 0; index < MAX_BULL; index ++)
{
Bullet *bullet;
bullet = new Bullet;
BulletVect.push_back(bullet);
}

That's OK but verbose. This is the equivalent.

for (int index = 0; index < MAX_BULL; ++index)
{
BulletVect.push_back(new Bullet);
}

Quote:

Also, I realize that pop_back uses the last element in the vector. I know it doesn't really matter but for understanding's sake, is there a function to use the first element?

No, you probably don't want to use pop_back(), which doe snot return a vlaue but just removes the last bullet in your list. There are handy accessor functions for the front and back of a vector: front() and back(). They return (references to) the values at the front and back, respectively.

To access something in the middle, either use operator[](size_type) or use operator*() or operator->() on a vector iterator. Like this.

for (vector<Bullet*>::iterator it = BulletVect.begin(); it != BulletVect.end(); ++it)
{
if (!it->is_dead())
{
it->draw();
}
}

Share this post


Link to post
Share on other sites
Why pre-allocate your vector with 100 bullets? You could get away with a simple array and element count if you are imposing an upper limit on the number of instances.

The point of a vector is to store a dynamic number of objects. So at the start of your game there are 0 bullets. During the game you add bullets as various actors in your game shoot.

For example:

class Bullet
{
// whatever members ...
};

int main()
{
// other init code...

std::vector<Bullet> bullets; // starts with 0 bullets

while(running)
{
// handle player input

if( key_pressed(SPACE_KEY) && player.canShoot() )
{
Bullet bullet = player.shoot();

bullets.push_back(bullet);
}

// maybe we have a vector of enemies that return fire

for( int i = 0 ; i < enemies.size() ; i++ )
{
if( enemies[i].canShoot() )
{
Bullet bullet = enemies[i].shoot();

bullets.push_back(bullet);
}
}

// game logic

for(int i = 0 ; i < bullets.size() ; i++ )
{
bullets[i].move();
}

// we would have a similar loop to move the enemies

/*
another loop here to remove bullets that have hit objects
or have gone beyond the bounds of the screen
*/


// drawing

for(int i = 0 ; i < bullets.size() ; i++ )
{
bullets[i].draw();
}

// draw the player and opponents
}
}


One point about the above code: no pointers. You do not need pointers in this case so I would heavily advise against using them. They will complicate your code for no benefits.

In general, if you aren't dealing with instances of extremely large objects, external APIs or polymorpism then you probably don't need to use raw pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by MeritGamer
vector <Bullet*> BulletVect;

#define MAX_BULL 100 // The error is here:
7 C:\Users\Family\Documents\FinalProject\Include\Headers\globals.h expected constructor, destructor, or type conversion before '<' token


Let's try reading this.

"before '<' token" means that the compiler couldn't figure out the thing that appeared immediately before a < symbol (a 'token' is any character or sequence of characters that forms a single "unit" in the source code: e.g. an identifier name, a semicolon, an opening or close bracket, an operator (many of which use two characters)...). For there to be a problem before the < symbol, there must be a < symbol. There's no such thing on the #define line; the problem is thus with the previous line (for various reasons, the compiler's idea of line count often differs from yours). The thing before the < is 'vector'.

The compiler "expected constructor, destructor or type conversion" before the <. Its reasons for expecting what it does are complex and often not easily understood, but the point is that it *didn't* expect to see 'vector'. Why not? Because at this point, it doesn't know what 'vector' means.

The standard library defines a 'vector' class, but (a) it's put in the std namespace, and (b) the standard library isn't automatically assumed to be in use; you have to pick and choose the parts that you need/want.

So check:

1) Is <vector> previously #include'd, so that the definition of std::vector is available?
2) Have you previously declared "using std::vector" or "using namespace std", so that the compiler knows to look within 'std' to find the name 'vector'?

Share this post


Link to post
Share on other sites
Ok, I understand the jist of what youre saying but i think its confusing me a bit more, so i am goign to stick with my way, the only thing I really need to know is how to display an element of the vector. Element of which containing the update draw dead functions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this