how do I add module?

Started by
11 comments, last by synth_cat 17 years, 9 months ago
I know that asking this question must make me look very dumb, but I have never actually done this before... I am working in Visual C++ 2005 Express. My game has a single main .cpp file and a bunch of headers (including enum calls, #defines, and class definitions.) The header files that define classes currently contain the class definitions _and_ the full method descriptions. (like so):

//welcome to SomeClass.h
class SomeClass
{
    void SomeClassMethod();
};

SomeClass::SomeClassMethod()
{
   //whatever
    return;
}

I've seen that people usually put the methods of their classes in separate .cpp files. How do I do that, exactly, and how do I connect the new .cpp files to the project? I obviously can't just say " #include SomeClass.cpp " in my main .cpp file. By the way, is there anything actually wrong with the organization I currently use, or is it just out of vogue? Thanks!
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
Advertisement
Normally you put definitions in your header file of the class and the descriptions in your cpp file.

Example:

//Class.h//Here you can put another headers that you needclass Test{    .    .    .}//Class.cpp//Here you can put also another headers that you need#include "Class.h"Test::Test(){}...


Thats that simple
-----"Master! Apprentice! Heartborne, 7th Seeker Warrior! Disciple! In me the Wishmaster..." Wishmaster - Nightwish
Quote:Original post by synth_cat
I know that asking this question must make me look very dumb, but I have never actually done this before...


Then why not post in For Beginners?

Quote:
I've seen that people usually put the methods of their classes in separate .cpp files.


Yes; you *must* either do this, or cause the function to be marked inline (either by putting it inline in the class definition, or by marking it with the 'inline' keyword). Or put the class definition into the .cpp instead; but that *only* works if *no* other "modules" need to "see" the class definition (if they don't/shouldn't, then by all means do this! As a general rule, put as absolute-little in your header files as you can get away with. Narrow the interface.

Quote:How do I do that, exactly, and how do I connect the new .cpp files to the project? I obviously can't just say " #include SomeClass.cpp " in my main .cpp file.


From the .cpp, as well as any other source file that makes use of the class definitions, include the .h. Tell the compiler to compile the .cpp, and to link the resulting object file (.o, .obj etc.) when creating the executable. The details of how to do that depend on your compiler and/or IDE (if any).

Quote:By the way, is there anything actually wrong with the organization I currently use, or is it just out of vogue?


See above. If more than one other module needs to use SomeClass, you WILL get "multiple symbol definition" linker errors (assuming you succesfully informed the compiler/IDE that SomeClass is in the project), EVEN IF you have proper include guards on your header file (which you should put anyway).
If you have a project in Visual C++, linking your .cpp files into the program is trivially easy. Just select "Project"->"Add New Item..." in the menu, and select "C++ File (.cpp)" in the list. Then you can just type up your implementation of the class (don't forget to #include the header file!).

When you hit "Build", Visual C++ will automatically link in all the C++ files that are included in your project.

Of course, it's also a good idea to learn how to put together and link a project the old-fashioned way (i.e., using command-line tools and makefiles), but if you're using Visual Studio, this is the easy way to do it. [smile]
So when I have created "MyClass.cpp" and added an #include "myclass.h" to it, do I still have to add a #include "myclass." to my main .cpp file?

Quote:
If more than one other module needs to use SomeClass, you WILL get "multiple symbol definition" linker errors (assuming you succesfully informed the compiler/IDE that SomeClass is in the project), EVEN IF you have proper include guards on your header file (which you should put anyway).

I don't really understand what situation this is describing. Could you give me an example? And what are the "include guards" for a header file, anyway? I thought include guards were used to restrict #include-ing a header file more than once (which seems to be somewhat out of context, unless there's something else I'm missing.)
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
All the .cpp files in your project will get compiled and linked together automatically by the editor, so don't worry about that. Just make sure you have the proper headers included in all the cpp files

Also, make sure to use inclusion guards in all your headers. #pragma once a t the top of all headers will do the trick, it will stop you from getting multiple inclusion errors.
in VS2005, once you have a project started, these three are your friend:

Project->Add Class...
Project->Add New Item...
Project->Add Existing Item...

You may or may not have noticed that there is a file with a vcproj extension. This is your project file. It is actually an XML file, and contains all of the files in your project, including which ones to compile.

Basically, if your cpp file shows up in the project explorer, it'll be compiled.

Headers don't have to show up in the project explorer, but it is useful to have them there for easy browsing.

Get off my lawn!

So let me just see if I've understood this:

I create a .cpp file by way of Project->Add Item that contains the implementation of my class's methods, and call it "myclass.cpp" Then I add the line "#pragma one" to the tops of all my .h files. Then I add "#include myclass.h" to both "main_file.cpp" _and_ "myclass.cpp."

Does that sound about right?
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
Alright, I did what you guys told me to do. I added new .cpp files to hold the function definitions for my three main classes. I also added the line "#pragma once" to the top of all my class definition header files.

It didn't exactly work: the program would not build and I got sent about 400 errors. The problem is that now the new .cpp files need certain #includes to be valid, but it doesn't work properly because the new .cpp files are compiled _before_ my main .cpp file.

Let me simplify all this and create a hypothetical situation so that I can ask my questions effectively (I am very sorry if this gets a bit long - it's hard to state this briefly):

1) I have two classes in my program: MyClassA and MyClassB

2) I have two .h files: one for each class. Note that at this point the methods are defined within the .h files!

Here is "myclassa.h"
class MyClassA{public:   void MethodA();};void MyClassA::MethodA(){     //whatever}


Here is "myclassb.h"
class MyClassB{public:     //note the line below!     MyClassA* mclassa_pnt;     void MethodB();};void MyClassB::MethodB(){     //whatever}


3) Both classes require information defined in another header file called (for simplicity) "generaldefines.h" This file contains macros, structs, enums, or whatever. Also, both classes contain things like D3DXVECTOR3 (in other words, d3d9.h and d3d9x.h need to be included before the compiler looks through myclassa.h and myclassb.h. Let's also say, for laughs, that the myclassa.h and myclassb.h perform std::string operations, so #include <string> must occur before they are looked at.

4) MyClassB contains a MyClassA pointer (see above files.) Thus, MyClassA _must_ be defined before the compiler even looks at myclassa.h

So the top of my main .cpp file looks something like this:
#include <windows.h>#include <d3d9.h>#include <d3dx9.h>#include <string>#include "generaldefines.h"#include "myclassa.h"#include "myclassb.h"using namespace std;//main loop, etc....


As you can see, this all works OK because myclassa.h and myclassb.h are included _after_ the compiler has gone through d3d9.h, d3dx9.h, string, and generaldefines.h. Also, there is no problem created by MyClassB requiring a pointer to MyClassA because #include "myclassa.h" is called before "myclassb.h"

Then, however, you guys told me that this structure was wrong, that my class methods should be defined in their own .cpp files. So I added myclassa.cpp and myclassb.cpp. I added #include "myclassa.h" to the top of myclassa.cpp, and #include "myclassb.h" to the top of myclassb.cpp. Now that the two header files were getting included twice (in the main .cpp and in either myclassa.cpp or myclassb.cpp), I added the line "#pragma once" to the top of myclassa.h and myclassb.h.

When I tried to run this whole thing, all heck broke loose. The problem was obvious: myclassa.cpp and myclassb.cpp were compiled before my main .cpp file. Thus, the info from d3d9.h, d3dx9.h, generaldefines.h, and string was not yet defined when the compiler started to read through myclassa.cpp and myclassb.cpp.

So here's my question: Is it OK to add this to the top of my myclassa.cpp file?
//I'm not sure if I'm doing this #ifdef check correctly, but I figured it must be necessary since I don't know if the file d3d9.h has the line #pragma once at the top of it#ifndef d3d9.h#include <d3d9.h>#endif#ifndef d3dx9.h#include <d3dx9.h>#endif#ifndef string#include <string>#endif#include "generaldefines.h"#include "synthgame.h"

and do the same thing for myclassb.cpp, including a the line "#include myclassa.h" so that MyClassB's MyClassA* value would be valid within myclassb.h?

This is what I have currently done, and my program does build with no errors, but I'm uncomfortable with this and want someone's approval. Did I do this right? Is it OK to call all those defines in multiple .cpp files? Did I do my "#ifndef" check correctly? Will I have to use it every single time I #include d3d9.h or string, since now I have multiple .cpp files and I'm never sure which order they will compile in?

Any help would be greatly appreciated! (I'm sorry I was so long-winded here.)
Greg Philbrick, Game Developercoming soon . . . Overhauled CellZenith
You might find this article useful: Organizing Code Files in C and C++

This topic is closed to new replies.

Advertisement