Working with Multiple File

Started by
3 comments, last by 1kevgriff 23 years, 7 months ago
Hi! I''ve been working on a project. I have a section of the game where the player can ask for hints based on certain flags that have been set (it''s an adventure game). I decided to write the hint system as a seperate file (hintsys.cpp). Well, I want to call functions from hintsys.cpp from my main() - which is in a file called Main.cpp. It''ll try to compile, but I get errors when i try to call a function from hintsys.cpp. Can anyone give me any advice or tips about using multiple files together? Thanks
Advertisement
Are you defining your functions as externals in a header file?

eg:

in Main.cpp you have:

#include hintsys.hpp
int main(void)
{
// Blah
}

int foo1()
{
// Do some stuff
}

-=-=-=-=-=-=-=-=-=-=-=-

In hintsys.cpp you have:
int foo2()
{
// Blah
}
-=-=-=-=-=-=-=-=-=-=-=-

In hintsys.hpp you have:

extern int foo2();

-=-=-=-=-=-=-=-=-=-=-=-

Is that your problem, or are you having problems calling a function from main from hintsys? In which case, at the top of your hintsys.cpp put in:

extern int foo1();

Where foo1 is the name of your function. It has to be an exact redeclaration to work though

That help?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-Chris Bennett of Dwarfsoft
"The Philosophers' Stone of Programming Alchemy"
IOL (The list formerly known as NPCAI) - A GDNet production
Our Doc - The future of RPGs
Thanks to all the goblins over in our little Game Design Corner niche
Dont forget to use this in your header file:

#ifndef HEADERNAME_H
#define HEADERNAME_H

//rest of file

#endif

To watch for multiple inclusion. (replacing HEADERNAME with the real name of the header file.

- Daniel
VG Games
- DanielMy homepage
Thanks.

I didn''t know I had to use

extern int GetHint(); // Function which gets the correct hint

But I added it, and the code worked perfectly. Thanks alot!
Hmm... if you''re using that in a header file, you don''t need the ''extern'' for functions. Just include the normal function prototype. But I suppose if either way works, there''s no point in changing it, is there?

-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"

This topic is closed to new replies.

Advertisement