Advertisement

Scoping Classes

Started by July 02, 2000 11:46 AM
4 comments, last by Twig Meister 24 years, 5 months ago
I''ve made a class with it''s own .h and .cpp file. An array of this class is then declared in another .h file. This "Global.h" file is then included within other .h files as I need access to it. However, this has caused me two major problems :- 1) How do I tell the compiler that the the array of classes is the same as in the other files. example // Class.h class Example { public : void SetNumber(int Number) ; int GetNumber() ; protected : int Number ; // Assume that Class.h has a Class.cpp with the member function declared correctely } // Global.h #include Class.h Example Array[5] ; // File1.h #include Global.h // File.cpp Array[0].SetNumber(5) ; // File2.h #include Global.h int Temp ; // File2.cpp Temp = Array[0].GetNumber ; IF I use this method then the Array[0] doesn''t seem to store any of the values I place into that array, which leads me to the second problem 2) If I use the extern command then I get the compiler message "Structure required on lefthand side of . or .*." Please help me this has been buggin me for weeks now
If you declare the array in the header file, then each .cpp will get its own copy of it. I''m surprised it even linked. you need to put the extern in the header file, and then declaration in just one .cpp:

in Global.h
extern Example Array[5];

in Global.cpp
Example Array[5];
Advertisement
How about the RTL? Errno seems to be declared in the header file and MSVC++ (maybe it''s compiler specific) assumes that it is defined only once and for everything else assumes extern (maybe because variables are automatically extern if static isn''t specified).
VK
Anon. Poster. Thank you for your help that solved my problems.
So what compiler were you using?
VK
C++ Builder 3

This topic is closed to new replies.

Advertisement