How to declare global variable in VC++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to declare global variable in VC++

  1. #1
    Guest

    How to declare global variable in VC++

    I have a problem in defininig global objects. The linker errors are faced. How to avoid it? Please suggest urgently


  2. #2
    Join Date
    Jan 2000
    Posts
    3

    Re: How to declare global variable in VC++

    It seems ur objects are not linked with other files.

    For global objects u can make extern declarations in one file and u can define and use in other file.



  3. #3
    Join Date
    Mar 2000
    Location
    Maine
    Posts
    68

    Re: How to declare global variable in VC++

    This works for me:

    In your main application header file decare your variable as extern.
    In the main app implementation file declare and define your variables.

    Like this:

    [ccode]
    //in MyApp.h
    extern int global1;
    extern char global2;
    //etc...Put initial declarations here


    //in MyApp.cpp
    //declare and define globals here

    Josh Knox
    Maxym Technologies

  4. #4
    Join Date
    Mar 2000
    Location
    Maine
    Posts
    68

    Re: How to declare global variable in VC++

    This works for me:

    In your main application header file decare your variable as extern.
    In the main app implementation file declare and define your variables.

    Like this:


    //in MyApp.h
    extern int global1; //extern implies that these variables will be define elsewhere
    extern char global2;
    //etc...Put initial declarations here


    //in MyApp.cpp
    //declare and define globals here
    int global1 = x;
    char global2 = "a";


    //these should now be avaiable globally




    hope this helps.

    Josh Knox
    Maxym Technologies

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