Advertisement

multiple .cpp files for newbies

Started by September 12, 2000 09:59 AM
22 comments, last by Newbie-O-Rama 24 years, 3 months ago
Ok here it goes...my very short tutorial...

create a project that contains the files that you needed...Do not put actual code in the .h files. The .h file only declare the functions or classes instead create two more .cpp files and title them the same as the two .h files you have. To complete this you need to declare in the .cpp files that you have declared the function in a .h file so at the begining of the new .cpp files just add "#include "File.h". Ok now if your reading this and going duh then here is an example

//Main file

#include "Functions.h"
#include "Variables.h"

#include // or what ever it is spelled like! I don''nt
// use dos mode anymore

int main()
{
// Bla bla bal
int Var;
DisplayText("Hello World");
Var = GetVar();
return 1;
};

//End of file

// Functions.h

void DisplayText(char text[]);

// Functions.cpp

void DisplayText(char text[]);
{
cout<};

// Variables.h

int GetVariable();

// Variables.cpp

int GetVariable();
{
return 5;
};

// End of file

If this does not help just go ahead and jump in that lake or give me an email and I will try to help you out with some more specific questions


"If I wanted to hear the pitter patter of little feet I would put shoes on my cat!"- One Great Programmer (Garret Foster)
Ok you know what just ignore those two posts!!! that is garbage (why it sent 2??????????) and why it says annonomous??!!!!! The text box must have deleated a bunch of stuff...email me and I will send you a project over the inet for you to compile or just look at for refrence. Like I said just Email me and I will fix your problem. If anyone looking at the previous two post reviews those as my capabilities as a programmer...please don''t! that was the forum''s fault I will fix as soon as possible!

"If I wanted to hear the pitter patter of little feet I would put shoes on my cat!"- One Great Programmer (Garret Foster)
-----------------------------------------------------------"People who usualy use the word pedantic usualy are pedantic!"-me
Advertisement
basically, you put the code in .cpp files, and prototypes and such in .h files..
following ecko''s example
    //file1.cpp#include <iostream.h>#include "file2.h"int main(void) {   cout << func(1,2) << endl;   return 0;}//file2.h#ifndef FILE2_H   //to prevent multiple inclusion...#define FILE2_Hint func(int a,int b);#endif//file2.cppint func(int a,int b) {   return a+b;}    

(of course) the output would be 3
the #ifndef stuff is important, but kinda confusing.. i can explain it if you want, but i''d rather not get into it until this problem''s cleared up...

-------------------
LPUNKNOWN pUnkOuter
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
humm, do U prototype your functions? For example:

#include

int DrawStuff( char sometext[50] );

int main()
{

DrawStuff( "Blah Blah Blah" );

return ( 0 );

}

int DrawStuff( char sometext[50] )
{

// text drawing code goes here

}

// end of file

Now that would probably work even if you left out the DrawStuff prototype at the start of the file, but if the function is in another file ( that I presume is linked in with the project ), then U have to prototype it and include the keyword extern in front. Assume the previous example was in two seperate files, then it would have to look like this:

// main.cpp

#include

extern int DrawStuff( char sometext[50] );

int main()
{

DrawStuff( "Blah Blah Blah" );

}

// end of file

// drawing.cpp

int DrawStuff( char sometext[50] )
{

// text drawing code goes here

}

// end of file

In this case the DrawStuff function is in a seperate file, meaning that for the main.cpp file to know anything about it, it has to be referenced, which is done by the line:

extern int DrawStuff( char sometext[50] );

Well, hope that helps!

Regards

~Spike
** Have I gone blind or has the world gone black? **
~SpikeYou can contact me at luke_howard@dingoblue.net.au
the unresolved external symbol thing is a linking error..
it means that when you compile the .cpp files, there''s no problem, ie. the prototype for the functions in other files are there.. maybe when you link you aren''t linking all of the object (.o or .obj) files? what compiler are you using? from the errors posted, i assume VC, which should be linking them together for you...
perhaps there are typos or something? for instance, the prototype might be

void myFunction();

but it may be implemented as

myFunction() { /*stuff*/ }

those two are not the same, the second is really int myFunction()

-------------------IUnknown *pUnkOuter
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
Thank you all for your help.
I still can''t get it to work, yes, i''m using VC6, but i think it hates me. I deleted the whole thing. I''ll try again later maybe, but right now it has me too flumuxed.
Advertisement
I hade the same problem once and when I changed in project settings to not use precompiled headers it worked.
Lemme see if I can try.

"Project|Add Files to Project", then add All *.cpp, *.h, and *.lib files your project needs. All of them. No exceptions. Well, one exception: the standard libraries, but I forget what those are called :p Otherwise, you''re doing everyting right.

BTW, multipile files are a pain but worth it if you reuse a lot of code (i.e. sprite classes and sound/music wrappers :D)

WhoopA, the Official Kicker of Butts

--- Home Page, visit at your own risk!
The future is not set. There is no fate, but what we make of ourselves.
Everywhere I look, I see rabbits. Maybe I should take down all my Sailor Moon stuff.
WhoopA, the Official Kicker of Butts--- Where have I been all these years?
I am writeing a game engine too and this is how I split it up into files.

lets say you have a project with these files in it..

g_main.cpp
int main();
cg_drawmenu.cpp
int drawmenu();
g_math.cpp
int dostats();
int upgrade();

If you use drawmenu() in any other file then g_main.cpp you must make this statement at the top of the file..
        extern int drawmenu();  /* This also works with variables. So if you need to use int health; in drawmenu() but it was defined g_math.cpp you can say */extern int health; //If the Variable is static, this will not work    


That help?



Edited by - Galileo430 on September 13, 2000 4:18:41 PM
------------------------------------------------------------I wrote the best video game ever, then I woke up...
If VC is set to use pre-compiled headers in your project, you must include the stdafx.h file in every .cpp file.


#include "stdafx.h"



Just put that code in each .cpp file and those errors should go away. Also, the exact error this problem will generate is: "error: unexpected end of file while looking for pre-compiled header directive" or something in similar tech-speak.

Happy Coding!


- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement