seperating decleration and definitions of classes

i created a game in sdl library, the game is working just fine but for the game i created few classes, i just wanted to get the game working as soon as possible, so i decleared and defined the ftns in the same header file, and used them in a single, globals.cpp file which i inlcuded in the main file so the thing arent that jumbled up, but now i have to seperate those definitions and while doing so, the definitions doesnt recognise the global varibles that i defined earlier/above any help or suggestions would be appreciated.

heres the global.cpp code its just inlcuded inthe main ftn
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
using namespace std;

//destination for the selected players imagese
SDL_Rect pic1;
SDL_Rect instrect = { 100,100,700,500 };

SDL_Window* gwindow = NULL;
SDL_Renderer* grender = NULL;

const int window_width = 900;
const int window_height = 700;


//keys info
bool keys[332];

enum Music_for { moves,special_moves };
enum State { main_menu, fighting };
State state;

//flag
bool is_running = true;
SDL_Event main_event;

void capfps(Uint32 starting_tick);

#include "music.h"
Music gb_music;
Music gfight_sound;
bool sound_flag = false;
Mix_Chunk* punch;
Mix_Chunk* kick;
Mix_Music* win;

//for players size
SDL_Rect grect = { 0,0,150,300 };
SDL_Rect g_mortal_combat_rects = { 0,0,100,260 };

//idle//walking//punch//kick//jump//death//special
double subzero_flags[11] = { 0 };

//idle//walkback//walkfront//punch//kick//jump//death//combo//
double ryo_flags[11] = { 0 };

//to know which players are selected
string player_selected[2] = { "" };

//if both are selected then onlh start the game
bool both_player_selected = false;

//variable for map selection
SDL_Texture* map = NULL;


void render(int x, int y, SDL_Texture* tex, SDL_Rect* clip);
SDL_Rect health1_rect;
SDL_Rect health2_rect;

bool won = false;
bool player1_won = false;
bool player2_won = false;

SDL_Rect death_rect;

#include "character.h"//this class needs the upper variables but when i 
//eperate the definitions of its members ftns, they cant find them

Character player1;
Character player2;

//texts to appear when a player is selected
SDL_Texture* subzero_selected = NULL;
SDL_Texture* lori_selected = NULL;

//for color keying
SDL_Surface* colorkey1 = NULL;

//winning text
SDL_Texture* win1 = NULL;
SDL_Texture* win2 = NULL;

//play again
bool again = true;
bool first_player_selected = false;
bool second_player_selected = false;

bool check_if_both_players_selected(SDL_Event event, State state);

void won_check();
Last edited on
You indeed have a problem.

The variables you declare (like player1, player2, win1, win2, ...) are declared every time you include that file. Can I assume it's globals.h or globals.hpp and not globals.cpp?

Here's what you need to do.
1. Put your code into git, so you don't loose anything.
2. You need a header file with your declarations, and a separate source file with your definitions.
3. Let's call this file globals.hpp
4. Make sure the rest of your program is using globals.hpp for this file.
5. Copy all those object declarations to a globals.cpp, and just include globals.hpp at the top.
6. Move all the prototypes to the top of the file, under the #includes.
7. Put extern in front of those declarations and remove the initial values, to make them definitions.
For example:
 
bool again = true;

becomes
 
extern bool again;

8. Add globals.cpp to your build system (or however your app is compiled).

That's it!
Possibly a little late for the suggestion, but it's always best to design the classes/structure/hierarchy/separation et al before you start coding.

"Code after design, not as design" as my programming professor used to tell us many, many, many moons ago.
Topic archived. No new replies allowed.