Archived

This topic is now archived and is closed to further replies.

function scope

This topic is 5885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I know this will sound stupid, but how can I have two files share functions with eachother? ex. \\a.cpp void functiona() { } void main() { functiona(); functionb(); } \\b.cpp void functionb() { functiona(); } I know this is probobly a really easy question, but I just can''t figure it out. Also I would like for a.cpp share its global variables with b.cpp and visa-versa. Thanx alot Zach ~ Tazzel3d

Share on other sites
Create two files called "a.h" and "b.h". These are header files which are used to solve this problem. In "a.h", write:
void functiona();

and in "b.h", write:
void functionb();

Then at the very top of "a.cpp" put this code:
#include "b.h"

and at the very top of "b.cpp" put this code:
#include "a.h"

This should solve the problem.

Share on other sites
Now I want the functions in b.cpp to be accessable from b.h

How would I accomplish that?

Zach ~ Tazzeld

Share on other sites
To share global variables you have to declare them extern in the header (.h) file.

a.h

extern int varA;

a.cpp

int varA;

Note that both a.cpp and b.cpp could #include the same header.h file containing all the function prototypes and variable definitions.

Share on other sites
I think, (if i understood the question) that you need to include both .h files in both .cpp files. Thus in "a.cpp" you have:

#include "a.h"
#include "b.h"

and the same thing in "b.cpp"

Share on other sites
I have both includes in both files, but the compiler wont let me call a function bb from b.h that is located in b.cpp

Basically, I want every function and variable, in file a callable from file b and visa versa.

Thanx for the help again

P.S. At least we are getting somewhere

Share on other sites
Err... in a.h and b.h you should only put function and variable declarations, not definition (i.e. functions with a body and non-extern variables). Headers are only there to tell the compiler that the function exists, not to tell it what it does (at least in the basic case, inline and templates (WHEN ARE WE GOING TO GET A WORKING EXPORT KWD !!!) are a different problem).

So you do

-- in a.h
  // declare functions from a.cppvoid functionA( int );// declare shared variables from a.cppextern int varA;

-- in a.cpp
  // include the headers#include "a.h"#include "b.h"#include // define variablesint varA = 0;// define functionsvoid functionA( int level ){ varB--; if (level > 0) { std::cout << varA functionB( level - 1 ); }}

-- in b.h
  // declare functions from b.cppvoid functionB( int );// declare shared variables from b.cppextern int varB;

-- in b.cpp
  // include the headers#include "a.h"#include "b.h"#include // define variablesint varB = 255;// define functionsvoid functionB( int level ){ varA++; if (level > 0) { std::cout << varB; functionA( level - 1 ); }}

Edited by - Fruny on January 13, 2002 1:23:42 PM

Share on other sites
I am really sorry about that, I had the whole fuction in the .h files, not just the decleration.

Thanx alot for putting up with me!!!

Share on other sites
Now I get the compiler telling me that all my variables are already in main.obj, co won''t put them in OpenLevel.obj also. I found I can get out of this problem by making the variable static, but is this really what I want to do?

Thanx aaaalllllooooottttt!!!!

Zach ~Tazzeld

Share on other sites
In C, declaring a variable static means that it is local to the file being compiled. Which means that EACH .obj file will have a copy. If your variable 'static int foo' exists both in a.cpp and b.cpp, functions in a.cpp will modify a's copy, while functions in b.cpp will modify b's copy. I don't think this is what you want.

Have you made sure to declare variables 'extern' in the headers and only define (i.e. without extern) each of them in a single .cpp file ?

Edited by - Fruny on January 13, 2002 4:18:32 PM