Trouble including...

Pages: 12
I am starting to try and separate the small games I have learned to make into classes and separate files for constants and global variables etc...

I can't seem to get it right, for instance in the constructors of some of my units that appear use my constants, like x = SCREEN_WIDTH / 2;

Even though I include my class under the include for the constants they still don't have scope within my class files.

#include "constants.h" //tried with constants.cpp also
#include "hero.cpp"

Am I going about this all wrong?
closed account (1yR4jE8b)
You really shouldn't include .cpp files, only headers. There are various things that can cause external references to fail, could you post some of your code?
You know I first had only the header files included and was having this problem. Then I downloaded one of the games from the lazyfoo sdl tutorial page to see how he did it and he included all cpp files. So I figured I was doing it wrong and tried that way. Still my files don't have scope to the constants and globals file.

Yes I will try it again and post the code, thanks!
I start out with this to make sure I can get a blank screen up. It works fine, all in one file.

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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_BPP = 32;

SDL_Surface *display = NULL;

int main(int argc, char* args[]) {


    if( SDL_Init( SDL_INIT_EVERYTHING) == -1 ) return 1;

    display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if(!display) return 1;

    SDL_Flip( display );
    SDL_Delay( 2000 );

    SDL_FreeSurface( display );
    SDL_Quit();
    return 0;
}




Then I put the constants into the constants.h and the SDL_Surface into the globals.h This works fine too,

the globals and constants are getting scope in main.cpp
1
2
3
4
5
// constants.h

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_BPP = 32;


1
2
 // globals.h
SDL_Surface *display = NULL;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//main.cpp

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"

int main(int argc, char* args[]) {

    if( SDL_Init( SDL_INIT_EVERYTHING) == -1 ) return 1;

    display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if(!display) return 1;

    SDL_Flip( display );
    SDL_Delay( 2000 );

    SDL_FreeSurface( display );
    SDL_Quit();
    return 0;
}




Now I am adding a class for the Hero of the game.. hero.h and hero.cpp, this is where the problems are

coming in play...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 //hero.h
class Hero {

private:

    int x;
    int y;

public:

    Hero();

    int GetX() { return x; }
    int GetY() { return y; }

};


1
2
3
4
5
6
7
8
9
 // hero.cpp

#include "hero.h"
Hero::Hero() {

    x = SCREEN_WIDTH / 2;
    y = SCREEN_HEIGHT / 2;

}



then I tried includeing into the top of main both ways:
1
2
3
4
5
6
 // top of main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"
#include "hero.h" 


1
2
3
4
5
6
 // top of main.cpp second attempt
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"
#include "hero.cpp" 


When I run the code like this, I get this error:
error: 'SCREEN_WIDTH' was not declared in this scope
error: 'SCREEN_HEIGHT' was not declared in this scope

the errors light up the line in the Hero.h file where x = SCREEN_WIDTH / 2;

I just cant understand it, because I am looking at other examples with the code doing exactly this.. using

the constants from files include above it. How come mine is failing on me?
You have to include constants.h in hero.cpp.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 //hero.h
class Hero {

private:

    int x;
    int y;

public:

    Hero();

    int GetX() { return x; }
    int GetY() { return y; }

};


Do you see
1
2
#include "constants.h"
#include "globals.h" 

in here?
No. So how does the compiler? We don't know what compiler you are using or how you have it set up, so you have to figure that out by yourself.
Unless your "other examples" are also telling you which compiler they are using, which order they are linking everything (etc.), then you aren't copying them exactly. I suggest you put header guards in your constants and globals, and include them in every file you use one of them.
Basically what Peter said.
By header gaurds to you mean #pragma once?

I will try including in all files. I just thought that was wrong because the game I looked at their class files didn't include their constants etc.

I am using code::blocks and the mingw
lol. ok now I remember, I did try including the globals and constants files in the hero.h file. then I get this problem:

error: redefinition of 'const int SCREEN_WIDTH'|
error: 'const int SCREEN_WIDTH' previously defined here|
error: redefinition of 'const int SCREEN_HEIGHT'|
error: 'const int SCREEN_HEIGHT' previously defined here|
error: redefinition of 'const int SCREEN_BPP'|
error: 'const int SCREEN_BPP' previously defined here|
error: redefinition of 'SDL_Surface* display'|
error: 'SDL_Surface* display' previously defined here|
||=== Build finished: 8 errors, 0 warnings ===|
Read what I said. You need header guards.
ok that was because I didn't change back the include of hero.cpp to hero.h on main.cpp.

so those errors are gone, but this is the error that I kept ending up at:

multiple definition of `display'

because I had to put the globals file in the hero file also..
ughh.. now my globals.h file doesn't have scope to SDL/SDL.h and SDL_Surface is not recognized.....
Put a header guard on globals.h.... also...
LOL. What are you doing? Press control + a. Then control + c.* lol. Then click reply down below. Press control + v. Don't forget to submit.

Go.

And don't copy all our posts. Copy your code, haha.
Last edited on
You get these errors because you didn't use include guards. You should also not define the globals in header files. Instead make an extern declaration in the header (extern SDL_Surface *display;) and define it in a source file (SDL_Surface *display = NULL;).
Oh yeah. I meant include guards. Sorry about that. You can try doing what Peter says. But if you don't want to do the extra work (and you probably won't) then just make some include guards for every header file you make.

You don't even need globals or constants. You can put them into your main function and send them to the object you need. Whichever you choose.
Wrong:
1
2
// globals.h
SDL_Surface *display = NULL;



Right:
1
2
3
4
5
// globals.h
extern SDL_Surface *display ;

// main.cpp
SDL_Surface *display = NULL;


I say main.cpp, because it seems the best choice from the source files you're currently using, but it could go in any .cpp file in your project provided it is only defined in one of them.
Alright I think I got a handle on things... I will see in a minute when I try to include my timer class, I had similar trouble last time.

by the way thanks a lot everyon
Pages: 12