CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2005
    Posts
    1

    Question global variable in an mfc application ??

    where can i declare a global variable to use it in different classes. thanks

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: global variable in an mfc application ??

    Global variables need to be declared in the header and be defined in one .cpp file...e.g.
    Code:
    // global.hpp
    extern int iInt;
    
    // global.cpp
    int iInt;
    However, usually you would avoid using globals...why do you need one in the first place?

  3. #3
    Join Date
    Mar 2004
    Posts
    32

    Re: global variable in an mfc application ??

    in afxstud.h

    CString str1;


    then use as

    extern CString str1; where ever u want( other classes);

    *** enjoy in programming***

  4. #4
    Join Date
    Mar 2005
    Location
    On the Dark Side of Moon
    Posts
    86

    Re: global variable in an mfc application ??

    ravirams ,
    i am using connection variable in a way you said
    but i am getting following errors
    Code:
    Linking...
    DataBaseView.obj : error LNK2005: "class _com_ptr_t<class _com_IIID<struct _Connection,&struct __s_GUID _GUID_00000550_0000_0010_8000_00aa006d2ea4> > pConnection" (?pConnection@@3V?$_com_ptr_t@V?$_com_IIID@U_Connection@@$1?_GUID_00000550_0000_0010_8
    000_00aa006d2ea4@@3U__s_GUID@@A@@@@A) already defined in DataBase.obj
    DBFunctions.obj : error LNK2005: "class _com_ptr_t<class _com_IIID<struct _Connection,&struct __s_GUID _GUID_00000550_0000_0010_8000_00aa006d2ea4> > pConnection" (?pConnection@@3V?$_com_ptr_t@V?$_com_IIID@U_Connection@@$1?_GUID_00000550_0000_0010_80
    00_00aa006d2ea4@@3U__s_GUID@@A@@@@A) already defined in DataBase.obj
    Debug/DataBase.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.
    x8086

    Problem Is An Opportunity To Do Your Best.

    All Code Guru Member's
    Poll : -Please Vote For Code Guru Forum Programming competition


  5. #5
    Join Date
    Mar 2005
    Posts
    13

    Lightbulb Re: global variable in an mfc application ??

    Quote Originally Posted by x8086
    ravirams ,
    i am using connection variable in a way you said
    but i am getting following errors

    x8086
    What is your variable name?

    From the error messages you showed us, it could be caused by "multiple declaration of variable".

    All "extern" statements can be included in all ".h" or ".cpp":
    PHP Code:
    // global.hpp
    extern int iInt
    But there must be one normal declaration in either ONE (and only one) ".cpp" or in a ".h" in which the contents are used by only one ".cpp":
    PHP Code:
    // global.cpp
    int iInt
    if the normal declaration is made in a ".h" file, and this file is included by a ".h" that is included by many ".cpp", you will also get the same error.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: global variable in an mfc application ??

    Let it be simple:
    • You declare as variable as extern many times, as many times you wish to refer in source files.
    • You define the variable only once across all source files.
    Defining let the linker (not the compiler) put the variable into executable, at global level. If you 'define' a variable many times, linker will already find in other object file (since you may have defined in the respective source file) and will compain you the same.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Mar 2005
    Posts
    13

    Thumbs up Re: global variable in an mfc application ??

    As pointed out by Ajay Vijay, there are differences between declaration and definition, which I overlooked and probably messed up in my previous post, I am sorry about my misleading post, please refer to Ajay Vijay's post which is more precise and neat.

    p/s: thank you, Ajay Vijay

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured