Global Variables
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Global Variables

  1. #1
    Join Date
    Sep 2005
    Posts
    35

    Thumbs up Global Variables

    How will I declare a global variable in VC++? I have created a struct on one of my class (lets say class1) after the #include.... It was included in the Global folder in the class view tab but when I'm going to access it on a different class (class2), the error is "undeclared identifier". I've tried to put the struct on the header file of class1 but a linker error occured. How will i be able made a struct be accessible by all of my classes? Thanks, I'm really stuck

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Global Variables

    Without looking at your code, it is quite difficult to understand your problem. Anyway, since you have already declared the struct class1, you still have to create an instance. Therefore in your class1 header file, you have to declare the global instance of class1 as extern. After that, you have to include class1 header file in class2 cpp file.

    Code:
    // In class1.cpp file
    class1 myobject;  // Declaring a global instance.
    
    // In class1.h file
    struct class1
    {
        //...
    };
    
    extern class1 myobject;  // Allows other cpp file that include this header file to have access to myobject.
    
    // In class2.cpp file
    #include "class1.h"
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  3. #3
    Join Date
    Sep 2005
    Posts
    35

    Re: Global Variables

    Oh, ok, I got it. I have to put the struct{}; inside the header file then declare the extern on the .cpp file. Thanx a lot

  4. #4
    Join Date
    May 2012
    Posts
    57

    Re: Global Variables

    Quote Originally Posted by Kheun View Post
    Without looking at your code, it is quite difficult to understand your problem. Anyway, since you have already declared the struct class1, you still have to create an instance. Therefore in your class1 header file, you have to declare the global instance of class1 as extern. After that, you have to include class1 header file in class2 cpp file.

    Code:
    // In class1.cpp file
    class1 myobject;  // Declaring a global instance.
    
    // In class1.h file
    struct class1
    {
        //...
    };
    
    extern class1 myobject;  // Allows other cpp file that include this header file to have access to myobject.
    
    // In class2.cpp file
    #include "class1.h"
    Reviving an old thread but it's applicable to my question:

    Assume class2.cpp contains:
    Code:
        // class2.cpp
    
        #include "class1.h"
        #include "class3.h"
        #include "class4.h"
    
        void test ()
        {
            myobject.variable1 = 5;
        }
    When myobject is used in class2:

    1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?

    2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.

    Thanks,
    Raptor (who's writing his first C++ program)
    Last edited by raptor88; September 1st, 2012 at 04:46 PM.

  5. #5
    Join Date
    May 2012
    Posts
    57

    Re: Global Variables

    When myobject is used in class2:

    1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?

    2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.
    Found the answer for question-2.
    ..... Got an "already defined in ......" error when compiled.

    Any answers for question-1?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Global Variables

    Quote Originally Posted by raptor88 View Post
    1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
    Source files are just text files. You have to search for whatever definition you're lookiing for.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Global Variables

    Quote Originally Posted by raptor88 View Post
    2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.
    If class1.cpp and class3.cpp are separate compilable modules, no error will occur at compile time.
    Code:
    // class1.cpp
    class SomeClass
    { };
    
    SomeClass myobject;
    Code:
    // class2.cpp
    class SomeClass
    { };
    
    SomeClass myobject;
    When compiled separately, no error. So how are you getting two C++ source files to know what's in the other source file, unless you're erroneously including one CPP source file in another CPP source file?

    The error will come when you attempt to link. The linker will see the multiple definitions and give you a linker error (not a compiler error).

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    May 2012
    Posts
    57

    Re: Global Variables

    Quote Originally Posted by raptor88 View Post
    1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
    Quote Originally Posted by Paul McKenzie View Post
    Source files are just text files. You have to search for whatever definition you're lookiing for.
    Figured that there's no way to know other than searching for the definition in every file.

    Thanks for the confirmation.
    Raptor

  9. #9
    Join Date
    May 2012
    Posts
    57

    Re: Global Variables

    Quote Originally Posted by Paul McKenzie View Post
    When compiled separately, no error. So how are you getting two C++ source files to know what's in the other source file, unless you're erroneously including one CPP source file in another CPP source file?

    The error will come when you attempt to link. The linker will see the multiple definitions and give you a linker error (not a compiler error).
    A noob thing on my part. Should have said "compiled and linked". IOW, what happens when the green arrowhead to start debugging is clicked.

    Thanks,
    Raptor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center