Qbasicnews.com

Full Version: splitting c++ program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
im trying to put my routines for moving my enemys in a seperate file but dont know how! I put the function into a file "bob.h" and #included it but it brings up errors galore! Ive heard that you have to declare variables with extern or something. so far, my brief look for tuts on the matter have been fruitless. any advice/tuts would be helpful.

please excuse my ignorance :oops:
Did you compile both programs together, eg create a project and have both main.c and bob.c together, with both of em having the header file?
You just need .h files including the things from inside a module you want to share with other modules. Then you stick everything inside your project.

For example, if you have MEADA.C that has a function called devuelve_numero_de_chingadas (int id_chingada); that you need in MAIN.C (your main module), create a file called MEADA.H with this content:

Code:
#ifndef _MEADA_H
#define _MEADA_H

void devuelve_numero_de_chingadas(int id_chingada);

#endif

then include in MAIN.C this line:

Code:
#include "meada.h"

I think I explained this a bit ago... look in another posts about C in this subsection Wink
How educative, Nate. ¿Quién te enseño? ¿El mismo Domingo que dice que los puntos son en 3D?

[Image: caritas.gif]
BTW, the archived images of your "celebration" are funnily disturbing. ¡Salú! [Image: beerchug.gif]
Ese mismo.
Ive tried what you said nathan and i get an error about 'out_func()' not being declared....

Code:
main.cpp

#include <iostream.h>
#include <stdlib.h>

#include "exmod.h"

void main()
{

   cout<<"in main module..."<<endl;
   out_func();
   system("PAUSE");
}

Code:
exmod.cpp

void out_func()
{
   cout<<"yay, a function from another file!!!";
}

Code:
exmod.h

#ifndef _exmod_H
#define _exmod_H
void out_func();
#endif
I copied your proggies and it compiled fine for me (well, I had to #include <iostream.h> in the external module 'cause cout is used there). Did you create a project and include the *.cpp files inside?
oh, you have to create a project.......
my bad! I got it to work now thanx Big Grin

now how can i use variables/consts/arrays declared in the main file in the function from the included file.

I know that I can pass it as an argument but that really wont quite suit my purpose for my program....
Well, create a .h file and declare those variables _THERE_ and then #include that file from any module that has functions that will use those variables. Anyways, I'd suggest you to use extra parameters 'cause that way you can keep track of variables better.
thanx for the help. I really must start using a better coding style, im getting into bad habbits!! :roll: