Thread: defining and using a global class

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    defining and using a global class

    how do I create and use a global class? I have only created global variables, never a class; I tried to use the same formate but came across errors.

    what I tried to do was declare the class at the top of my driver file:
    Code:
     (driver file)
    #include <normalStuff>
    
    class MyGlobalClass gClass; // IS THIS HOW I DECLARE A GLOBAL CLASS 
    
    class DriverClass{
    private:
         ... // normal stuff
    public:
         void start(void);
         ...// normal stuff
    };
    
    int main(void){
         DriverClass dc;
         dc.start();         // this is where I will put values in the global class
         ...blah blah blah
      }
    Code:
    (some other file who wants to access the global class)
    #include <normalStuff>
    
    extern class MyGlobalClass gClass; // IS THIS HOW I GET ACCESS TO THE GLOBAL CLASS? 
    
    void class2::function(){
          double temp;
          temp = gClass.data;   // THIS IS WHAT GIVES ME AN ERROR 
      ...
    }
    chris

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    What is "data"??? Is it a function? An attribute?
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    38

    whoops

    sorry I forgot to add that

    data would just be a typical element of the data class (such as "double");

    Code:
    class MyGlobalClass{
    private:
          ;
    public:
         ... //typical functions
    
          double data;
    };
    i realize that I should make "data" a private member and just add get/set functions,but for this example I will just forgo that
    chris

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Remove "class" from the following:
    Code:
    class MyGlobalClass gClass;
    and
    Code:
    extern class MyGlobalClass gClass;
    And also make sure you include the header file that defines MyGlobalClass in all files that use it. Otherwise, you're just making a forward declaration of an incomplete type.
    Last edited by thefroggy; 03-05-2004 at 02:03 PM.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Do not forget to put the extern keyword in the .h file, and the real definition at only one .cpp. Otherwise, you will get linker errors: multiple definition.
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed