kampi
kampi

Reputation: 2484

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 to solve me this problem? Where should i declare these variables and functions, so they can "see" and "use" each other?

Thanks!

Upvotes: 0

Views: 53

Answers (1)

Eran
Eran

Reputation: 22020

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.

Upvotes: 1

Related Questions