Archived

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

Dwiel

function scope

Recommended Posts

Dwiel    365
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 this post


Link to post
Share on other sites
DanTheRocker    122
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 this post


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

How would I accomplish that?

Thanx for the quick reply

Zach ~ Tazzeld

Share this post


Link to post
Share on other sites
Fruny    1658
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 this post


Link to post
Share on other sites
DanTheRocker    122
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 this post


Link to post
Share on other sites
Dwiel    365
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 this post


Link to post
Share on other sites
Fruny    1658
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.cpp

void functionA( int );
// declare shared variables from a.cpp

extern int varA;

-- in a.cpp
  
// include the headers

#include "a.h"
#include "b.h"
#include <iostream>
// define variables

int varA = 0;
// define functions

void functionA( int level )
{
varB--;
if (level > 0)
{
std::cout << varA
functionB( level - 1 );
}
}

-- in b.h
  
// declare functions from b.cpp

void functionB( int );
// declare shared variables from b.cpp

extern int varB;

-- in b.cpp
  
// include the headers

#include "a.h"
#include "b.h"
#include <iostream>
// define variables

int varB = 255;
// define functions

void 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 this post


Link to post
Share on other sites
Dwiel    365
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 this post


Link to post
Share on other sites
Dwiel    365
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 this post


Link to post
Share on other sites
Fruny    1658
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

Share this post


Link to post
Share on other sites