How to use a function and global variables in an other included .cpp file?
I have a little trouble with including and defining global variables and functions. I have my main .cpp file let's say test.cpp. I also have another .cpp file ( functions.cpp )included which contains a function, MyFunction(). My problem is, that MyFunction uses a global variable like HostName, which is defined in test.cpp, and also uses another function MyFunction2() which is also defined in test.cpp. My problem is that MyFunction can't "see" HostName neither MyFunction2.
Could someone help me t开发者_开发百科o solve me this problem? Where should i declare these variables and functions, so they can "see" and "use" each other?
Thanks!
Where should i declare these variables and functions?
In test.h.
In test.cpp, declare HostName
and . Then, in test.h, declare the following:
extern string HostName; // or whatever type HostName is
int MyFunction2();
Then, whoever includes test.h can use HostName
and MyFunction2
, keeping their definition in only one place.
精彩评论