Problem in multifile project

I am working on a multifile project, I used STL, SFML, Box2D. and planning to use boost...

A few days ago I encounter a problem that shouldn't be there
1
2
b2Body *init_body(){ ... }
b2Body *MyBody = init_body();

causes things to initialized in a wrong way
when the code reaches to the point that it needs MyBody variable
it produces a Run Time Error

Somehow the bug is fixed by
1
2
3
b2Body *init_body(){ ... }
b2Body *MyBody = NULL;
void init(){ MyBody = init_body(); }


call init() the first thing in int main()
and the bug disappear

Today, I tried to simulate again what happen out of curiosity
I went and change the code back to my first method

and the bug isn't there...
then I thought, why does that bug happened in the first place ?
Does the order of *.cpp file compile matters ?
Does the order of includes matters ?

and also if there is for example 5 files
a.cpp
1
2
#include "a.hpp"
int variableA = 3;


a.hpp
1
2
3
4
#ifndef AFILE
#define AFILE
extern int variableA;
#endif 


b.cpp
1
2
#include "b.hpp"
int variableB = 5;


b.hpp
1
2
3
4
#ifndef BFILE
#define BFILE
extern int variableB;
#endif 


main.cpp
1
2
3
4
5
#include "a.hpp"
#include "b.hpp"
int main(){

}


which variable will be declared first ?
is it variableA or is it variableB ?
and how can I tell ?
Last edited on
Topic archived. No new replies allowed.