SFML community forums
Help => General => Topic started by: Makuto on July 23, 2012, 05:50:11 pm
-
I'm splitting my large main.cpp into multiple files for better organization right now. What I was wondering was where I put the SFML include headers. Here's what my project looks like so far:
main.cpp //This is the file that contains all my code
graphics2D.h //These are the files that my code will be distributed into
graphics2D.cpp
soundAndMusic.h
soundAndMusic.cpp
networking.h
networking.cpp
Do I #include <SFML/component.hpp> in the component header and .cpp files, or just the header files? Do I need to include SFML in main.cpp, even if I'm only calling functions in the seperate modules?
-
I'd suggest to use *.hpp instead of *.h since you're using C++ and not C it will make it easier to determin which headers are written in C++ and which could be written in C. ;)
Since you always include the header file in the corresponding source file, you won't have to include both headers in the X.cpp and the X.hpp, but other than that you should never make assumptions like: 'I don't have to include it in X.hpp since I'm including Y.hpp which has alreay included the SFML header.' Because you never know if you'll include the header file in another file.
Next it's adviced to include as late as possible. For instance most of the time you'll never need the std::cout in a header file since the header files only hold the class decelerations, but you may use it in the coresponding source file. Thus you include it in the source file and not in the header file.
And finally if you're just using a pointer or a reference to a class X in a header file Y, you don't have include the header file for X but you can use a forward decleration (http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml) (e.g. class X;) and include the header of X later in the source file.
Overall it's just an optimization for compiling and not that important.
-
I had some problems with templates, but other than that, everything is working! Thanks for the help!
-
I had some problems with templates...
Yeah templates have to be defined in the header file. ;D