Reading same int or HWND etc from diffrent .cpp

Hello,

I'm trying to learn about header files,I know that I can use variables and definitions of functions in it, but one thing I can't understand is that how can 2 .cpp files share the same variable?

For example, if I write int iTest = 1; in main.h and include it in both main.cpp and functions.cpp it will cause error due multiple definition.

The main problem with this is if I want to send a message to a textbox in main.cpp from function.cpp(using WINAPI) it will not find the window because I can only assign HWND TextBox1; in one header and include it from one .cpp or else it will cause the multiple defintion error.
Last edited on
Heyas.

C++ has an extern keyword to deal with that. Extern is basically an indication that something is a declaration only, thus cutting down on redefinition errors. Note that you will have to define your variable in at least one of the files.

Example:
http://en.wikipedia.org/wiki/External_variable#Example_.28C_programming_language.29

Good luck!

-Albatross
Last edited on
Oh my.... programing can be easier than you expected sometimes :)

Thanks!
Topic archived. No new replies allowed.