Setting up headers and project files

Started by
4 comments, last by MadScientist 12 years, 7 months ago
So I'm an intermediate to advanced programmer and can program a good deal of things. But I still don't know how certain files should be used in a project to keep it organized. I know that cpp files have all the main code and headers have declarations and includes but I don't know how to arrange them. I have a main.cpp file that I use for the bulk of the code but where should I put the functions? In another .cpp file or a header file and if it is a .cpp file how do I properly include it so that the headers included in the header file can be accessed by the cpp file that isnt the main. I'm using visual c++.
If it hasn't exploded, I haven't touched it.
Advertisement
There's no one way to do it so you'll just have to find what works for you. What i'll do is write everything in main.cpp and when i see that several functions operate on a specific type of data, or do a specific subset of things, i'll move those functions to their own headers and sources. I do the same for classes.

For me, it's just easier to write and refactor than write it right the first time.
You can group functions that belong together in one file, a class can go into it's own file, if there are many small classes that belong together you can put them all in one file. For each source (.cpp) file you have header file (main.cpp is probably an exception and don't need a header file).

So say that you have an item class. You can have a header named item.h that contains the class definition and a source file named item.cpp that defines the member functions. To be able to use the item class from another file you include item.h.
So in your example, do I need to include the item.cpp anywhere or is it automatic?
If it hasn't exploded, I haven't touched it.
No you never include .cpp files. All you need to do is make sure all .cpp files get compiled and then linked together. Exactly how to do this depends on your compiler/IDE.
I'm currently using visual studio c++ 2008. Thanks! I got all of it working. Now to re organize my project....
If it hasn't exploded, I haven't touched it.

This topic is closed to new replies.

Advertisement