Jump to content
  • Advertisement
Sign in to follow this  
MeritGamer

Initialializer Lists

This topic is 3444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have no idea how they work. Amung other errors in this code, how do I use initializer lists in this case? ---------------------------------------------------------------------------- .cpp Bullet::Bullet() { //Initialize the velocity bXVel = 4; bYVel = 4; Bullet : (/*shooting*/) } void Bullet::handle_input() { if( event.type == SDL_KEYDOWN ) { switch( event.key.keysym.sym ) { case SDLK_SPACE: Bullet_Pointer->draw; Shooting = true; break; } if (Shooting == true) { Bullet_Pointer->update; } } } void Bullet::update() { bX += bXVel; bY += bYVel; } void Bullet::draw(SDL_Surface *bulletImage, SDL_Surface *screen) { SDL_Rect destination = {bX,bY}; SDL_BlitSurface(Bullet_Pointer,0,screen, &destination); } bool Bullet::dead() const { SDL_Surface *screen = SDL_GetVideoSurface(); if(bX > screen->w || bX < 0) { return false; } else if(bY > screen->h || bY < 0) { return false; } return true } }; ---------------------------------------------------------------------------- .h class Bullet { private: //The x and y offsets of the bullet int bX, bY; //The velocity of the bullet int bXVel, bYVel; bool Bbullet; public: Bullet : { void update(); void draw(); bool dead() const; void handle_input(); } };

Share this post


Link to post
Share on other sites
Advertisement
Your class definition has a few syntax errors. I assume you want to declare a constructor:

class Bullet
{
private:
//The x and y offsets of the bullet
int bX, bY;

//The velocity of the bullet
int bXVel, bYVel;

bool Bbullet;

public:
Bullet : // problem: not legal syntax

{ // problem: this shouldn't be here

void update();
void draw();
bool dead() const;
void handle_input();

} // problem: this shouldn't be here either
};


It should be:

class Bullet
{
private:
//The x and y offsets of the bullet
int bX, bY;

//The velocity of the bullet
int bXVel, bYVel;

bool Bbullet;

public:
Bullet();

void update();
void draw();
bool dead() const;
void handle_input();
};



Also note that you don't set the value of the members bX, bY and Bbullet in your constructor. You should.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Your class definition has a few syntax errors. I assume you want to declare a constructor:
*** Source Snippet Removed ***
It should be:
*** Source Snippet Removed ***

Also note that you don't set the value of the members bX, bY and Bbullet in your constructor. You should.


Thanks, now that I look back, I don't know what I was thinking, that is quite basic.

However I do have another issue. Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'

Same for update.

I know you had mentioned this earlier, but changing the pointer name to something more descriptive did not chnage anyhting.

Share this post


Link to post
Share on other sites
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Quote:
Original post by MeritGamer
Im getting an error on:
Bullet_Pointer->draw;
Bullet_Pointer->update;

94 C:\Users\Family\Documents\FinalProject\Include\Headers\classes.cpp 'struct SDL_Surface' has no member named 'draw'


What is the type of Bullet_Pointer? It looks like it's a pointer to an SDL_Surface, and you probably intended for it to be a pointer to your Bullet class.


Yes, Bullet_Pointer is pointing to an image. I diddnt know you can point to class. How would it look and work?

Share this post


Link to post
Share on other sites
I don't understand why a Bullet needs to react to input in the first place. In my mind, some other object (e.g. a Player object) might react to input to create a bullet.

E.g:

class Bullet
{
// bullet stuff
};

class Player
{
public:
// player stuff

Bullet shoot();
};

int main()
{
Player player;

std::vector<Bullet> bullets;

while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_SPACE:

bullets.push_back(player.shoot());
break;

// other keys

default:
break;
}
}
}

// update all objects

// remove dead objects from the simulation

// draw objects
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
I don't understand why a Bullet needs to react to input in the first place. In my mind, some other object (e.g. a Player object) might react to input to create a bullet.

E.g:
*** Source Snippet Removed ***


Well I am still not familier with vectors or the push.back function.

You can call Bullet(); into the "player" class? I never knew, I have a similar class relating to player, but instead it's just "Plane".

Share this post


Link to post
Share on other sites
Sounds like you need to learn more about how classes work. Check out the free C++ books in the links below.

Share this post


Link to post
Share on other sites
Quote:

Well I am still not familier with vectors or the push.back function.

A vector is a dynamic object container. That means that std::vector<Bullet> can hold many bullets. A vector is modelled on an array, so you can use array like semantics when dealing with one. Because the vector can contain any number of instances, you can ask the vector for its size to determine how many times to loop:

std::vector<Bullet> bullets;

// add some bullets to the vector

for(int i = 0 ; i < bullets.size() ; ++i)
{
bullets.update();
}



This is very simple usage.

The push_back function simply adds an element to the end of the vector.

std::vector<int> integers;

// integers is empty, so its size == 0.

integers.push_back(1);

// integers is {1}, its size is now 1 and integers[0] == 1.

integers.push_back(42);

// integers is {1, 42}, its size == 2.
// integers[0] is still 1.
// integers[1] is 42.

integers.push_back(13);

// integers is {1, 42, 13}, its size == 3
// integers[0] is still 1.
// integers[1] is 42.
// integers[2] is 13.

// This next bit prints
//
// integers are: 1 42 13
//
std::cout << "integers are:";

for(int i = 0 ; i < integers.size() ; ++i)
{
std::cout << ' ' << integers;
}

std::cout << '\n';




Quote:

You can call Bullet(); into the "player" class?

Well, once you have defined the Bullet class it is like any other type. You can treat bullet like you would int or std::string. So you can create a function in the Plane class that returns Bullet instances.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!