Jump to content
  • Advertisement

Archived

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

Coaster Kev

Working with Multiple File

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!