Calling functions in other files

Joined
Jul 31, 2014
Messages
836
(Really noob/basic question, I know :lol:, still new to C++ programming)

How would I call a function (let's say, 'GetMaxEffectiveCities(bool bIncludePuppets)' from the file CvPlayer.cpp), from a function in another file (i.e. CvTreasury.cpp)?

Would I have to #include CvPlayer.h at the top of CvTreasury.cpp? Would I need to do anything with CvTreasury.h?
 
GetMaxEffectiveCities isn't a "function" it's a method on the CvPlayer object (you can tell this because its signature is int CvPlayer::GetMaxEffectiveCities(bool bIncludePuppets)
and not GetMaxEffectiveCities(bool bIncludePuppets)

To call it you will need an instance of a player object, then use either pPlayer->GetMaxEffectiveCities(bIncludePuppets) or kPlayer.GetMaxEffectiveCities(bIncludePuppets) (depending if you have a pointer/reference to the object, or the actual object)

In terms of what you need to #include, there are very few instances where you need to include specific header files as they are all included in the pre-compiled header (.pch) file, which is included into (almost) all of the .cpp files/ Easiest way to find out if you need to explicitly include the header is to leave it out, then wait for the compiler errors! If there are some, you would need to #include CvPlayer.h into CvTreasury.cpp
 
Ah, I see. Thanks! So something like:

Code:
m_pPlayer->GetMaxEffectiveCities(bIncludePuppets)
?

(Using m_pPlayer since it shows up elsewhere in the same file).
 
Back
Top Bottom