Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why can't global fn access global variable?

why can't global fn access global variable?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    closecall
    wrote on last edited by
    #1

    i've declared int iGlob in a header file. then get error: error C2039: 'iGlob' : is not a member of '`global namespace' for this code: void myGlob() { ::iGlob = 123; } why? :confused:

    P C 2 Replies Last reply
    0
    • C closecall

      i've declared int iGlob in a header file. then get error: error C2039: 'iGlob' : is not a member of '`global namespace' for this code: void myGlob() { ::iGlob = 123; } why? :confused:

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      Did you extern the global variable in that .cpp file. The World is getting smaller and so are the people.

      C 1 Reply Last reply
      0
      • P Prakash Nadar

        Did you extern the global variable in that .cpp file. The World is getting smaller and so are the people.

        C Offline
        C Offline
        closecall
        wrote on last edited by
        #3

        i tried extern "C" { #include "_Globals.h" } and extern int iGlob; in cpp file.. but no luck. i know this is probably trivial stuff, how about an example? .cpp file: ... .h file: ... Dlg file: ... thanx:cool:

        P 1 Reply Last reply
        0
        • C closecall

          i've declared int iGlob in a header file. then get error: error C2039: 'iGlob' : is not a member of '`global namespace' for this code: void myGlob() { ::iGlob = 123; } why? :confused:

          C Offline
          C Offline
          closecall
          wrote on last edited by
          #4

          found the solution on CP's FAQ.... 6.2: How do I share a global variable among my .CPP files? (top) First, in one of your .CPP files (and only one) declare the variable at global scope (that is, outside of all function and class definitions). For example: int g_volume; Then in a header file that is included in all .CPP files - such as stdafx.h - add an extern declaration: extern int g_volume; The extern keyword tells the compiler that g_volume is an int declared in some other .CPP file. If you forget the first step, the linker will give an unresolved external error. :cool:

          1 Reply Last reply
          0
          • C closecall

            i tried extern "C" { #include "_Globals.h" } and extern int iGlob; in cpp file.. but no luck. i know this is probably trivial stuff, how about an example? .cpp file: ... .h file: ... Dlg file: ... thanx:cool:

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #5

            i dont konw which compiler you are using but this works for me. ---in global.h---- extern int uGlobal; ---in global.cpp--- int uGlobal; ---place where you will need that variable--- #include "global.h" void somefunc() { uGlobal = 55; } all the files must be in the project, compiled and linked. Hope i got you rite and answeredit correctly. The World is getting smaller and so are the people.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups