• Advertisement
Sign in to follow this  

SDL Muliple Declaration Error

This topic is 3315 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

So once again i have a dilema in my game. I recently got it too shoot, which is nice. Now I need a bit of animation. So, I have some code plugged in to do this for an enemy but it is saying that a variable is declared multiple times..but it is not, i have checked. here is my code. It's a bit messy and may not exactly do what i want but right now I just want it to compile and run. (The error comes just before runtime) Image.h> void set_clips(); Image.cpp> void set_clips() { //Clip the sprites clipsRight[ 0 ].x = 0; clipsRight[ 0 ].y = 0; clipsRight[ 0 ].w = Enemy1_WIDTH; clipsRight[ 0 ].h = Enemy1_HEIGHT; clipsRight[ 1 ].x = Enemy1_WIDTH; clipsRight[ 1 ].y = 0; clipsRight[ 1 ].w = Enemy1_WIDTH; clipsRight[ 1 ].h = Enemy1_HEIGHT; clipsRight[ 2 ].x = Enemy1_WIDTH * 2; clipsRight[ 2 ].y = 0; clipsRight[ 2 ].w = Enemy1_WIDTH; clipsRight[ 2 ].h = Enemy1_HEIGHT; } Globals.h> extern SDL_Surface *enemy1 ; SDL_Rect clipsRight[ 3 ]; const int Enemy1_WIDTH = 21; const int Enemy1_HEIGHT = 34; const int Enemy1_RIGHT = 0; Globals.cpp> SDL_Surface *enemy1 = NULL ; Classes.h> //The stick figure class ENEMY1 { private: int offSet; int velocity; int frame; int status; public: ENEMY1(); void handle_events(); void move(); void show(); }; Classes.cpp> ENEMY1::ENEMY1() { //Initialize movement variables offSet = 0; velocity = 0; //Initialize animation variables frame = 0; status = Enemy1_RIGHT; } void ENEMY1::handle_events() { //If a key was pressed if( event.type == SDL_KEYDOWN ) { //Set the velocity switch( event.key.keysym.sym ) { case SDLK_RIGHT: velocity += Enemy1_WIDTH / 4; break; case SDLK_LEFT: velocity -= Enemy1_WIDTH / 4; break; } } //If a key was released else if( event.type == SDL_KEYUP ) { //Set the velocity switch( event.key.keysym.sym ) { case SDLK_RIGHT: velocity -= Enemy1_WIDTH / 4; break; case SDLK_LEFT: velocity += Enemy1_WIDTH / 4; break; } } } void ENEMY1::move() { offSet += velocity; if( ( offSet < 0 ) || ( offSet + Enemy1_WIDTH > SCREEN_WIDTH ) ) { offSet -= velocity; } } void ENEMY1::show() { //If Foo is moving right if( velocity > 0 ) { status = Enemy1_RIGHT; frame++; } else { frame = 0; } } Thats it for the Enemy code. Just naturally gets called in the program, but that's it. These are the exact errors: multiple definition of `clipsRight' first defined here

Share this post


Link to post
Share on other sites
Advertisement
Firstly, read this. It should give you some clues as to the cause of the error how to prevent it in future.

However, there are more fundamental issues with your design.

A class called ENEMY1? On a stylistic note, most programmers reserver ALL_CAPS identifiers for the preprocessor. But other than that, you remember that a class serves as a blueprint for object instances? So a better name would be "Enemy". This way we can easily visualise having many enemy instances (which is normal, even if in the end we choose to only create one).

In Enemy::handle_events(), you are referencing the global "event". Don't do this, it is bad style. Instead, pass the event to the function:

// enemy header file
class Enemy
{
public:
// ... other functions ...
void handle_event(const SDL_Event &);
};

// enemy source file
void Enemy::handle_event(const SDL_Event &event)
{
if(event.whatever)
{
// ...
}
}

// main
int main(/* ... */)
{
// initialisation ...
Enemy enemy1, enemy2; // should be in an array, this is for demonstration

while(running)
{
// other stuff ...

SDL_Event event;
while(SDL_PollEven(&event))
{
enemy1.handle_event(event);
enemy2.handle_event(event);
}
}
}



Likewise, I am suspicious of the need for the SDL_Surface and clipping rectangles to be global. See if you can localise them and pass them to functions instead.

Finally, constants like Enemy1_WIDTH, Enemy1_HEIGHT and Enemy1_RIGHT can go right in the enemy class (losing the need for the prefix):

class Enemy
{
// ...
private:
static const int WIDTH = /* ... */, HEIGHT = /* ... */, RIGHT = /* ... */;
};




In fact - "status" should really be an enumeration:

class Enemy
{
// ...
private:
static const int WIDTH = /* ... */, HEIGHT = /* ... */;

enum Status
{
IDLE, LEFT, RIGHT, JUMPING, FLYING, SINGING; // plus any more
};
};


Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Firstly, read this. It should give you some clues as to the cause of the error how to prevent it in future.

However, there are more fundamental issues with your design.

A class called ENEMY1? On a stylistic note, most programmers reserver ALL_CAPS identifiers for the preprocessor. But other than that, you remember that a class serves as a blueprint for object instances? So a better name would be "Enemy". This way we can easily visualise having many enemy instances (which is normal, even if in the end we choose to only create one).

In Enemy::handle_events(), you are referencing the global "event". Don't do this, it is bad style. Instead, pass the event to the function:
*** Source Snippet Removed ***
Likewise, I am suspicious of the need for the SDL_Surface and clipping rectangles to be global. See if you can localise them and pass them to functions instead.

Finally, constants like Enemy1_WIDTH, Enemy1_HEIGHT and Enemy1_RIGHT can go right in the enemy class (losing the need for the prefix):
*** Source Snippet Removed ***

In fact - "status" should really be an enumeration:
*** Source Snippet Removed ***


I am curious, I have come across enum in my partners code. What doese that do? And what is a static const?

Valid points, when I am done playing guitar I will read through that throghly.

Share this post


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

  • Advertisement