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. Global Variable Problem

Global Variable Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
7 Posts 5 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
    Caoimh
    wrote on last edited by
    #1

    I need to initialise the global variables, so when I attempt the following, an error message appears.Any suggestions appreciated in stdafx.cpp file #include "stdafx.h" int g_SomeGlobalVariable = 0; and in stdafx.h file extern int g_SomeGlobalVariable = 0;

    M 1 Reply Last reply
    0
    • C Caoimh

      I need to initialise the global variables, so when I attempt the following, an error message appears.Any suggestions appreciated in stdafx.cpp file #include "stdafx.h" int g_SomeGlobalVariable = 0; and in stdafx.h file extern int g_SomeGlobalVariable = 0;

      M Offline
      M Offline
      Manfred Staiger
      wrote on last edited by
      #2

      Omit the "extern int g_SomeGlobalVariable" in the header file. You must use this "extern"-statement in the files from which you want to access your global variable (it's only for the linker). MS

      R 1 Reply Last reply
      0
      • M Manfred Staiger

        Omit the "extern int g_SomeGlobalVariable" in the header file. You must use this "extern"-statement in the files from which you want to access your global variable (it's only for the linker). MS

        R Offline
        R Offline
        Robert A T Kaldy
        wrote on last edited by
        #3

        You needn't to do that. Let the declaration in the stdafx.h (because stdafx.h is included in all source files generated by AppWizard), but omit the initialization ( = 0). Global and static variables must be initialized only in the source file. Robert-Antonio "Czech Railways discovered, that in case of disaster the most damaged wagons were the first and the last. So they decided to create trains without them."

        M 1 Reply Last reply
        0
        • R Robert A T Kaldy

          You needn't to do that. Let the declaration in the stdafx.h (because stdafx.h is included in all source files generated by AppWizard), but omit the initialization ( = 0). Global and static variables must be initialized only in the source file. Robert-Antonio "Czech Railways discovered, that in case of disaster the most damaged wagons were the first and the last. So they decided to create trains without them."

          M Offline
          M Offline
          Manfred Staiger
          wrote on last edited by
          #4

          I don't know if you are completely right. Certainly, you get an errormessage on an assignment in a header file. But in this case I wouldn't be amazed if the whole "extern"-statement is a problem, because his globalVar isn't "extern" in this case. I didn't try out. Anyhow, I wouldn't place an "extern"-statement in the stdafx.h, even because it is included everywhere else. But that's a question of the point of view. MS

          B T 2 Replies Last reply
          0
          • M Manfred Staiger

            I don't know if you are completely right. Certainly, you get an errormessage on an assignment in a header file. But in this case I wouldn't be amazed if the whole "extern"-statement is a problem, because his globalVar isn't "extern" in this case. I didn't try out. Anyhow, I wouldn't place an "extern"-statement in the stdafx.h, even because it is included everywhere else. But that's a question of the point of view. MS

            B Offline
            B Offline
            BIG_MG
            wrote on last edited by
            #5

            Another way of doing the same would be declare it static. So in the stdafx.h you can write like static int gi_var = 0;

            T 1 Reply Last reply
            0
            • B BIG_MG

              Another way of doing the same would be declare it static. So in the stdafx.h you can write like static int gi_var = 0;

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              That wouldn't be a true global variable then. That makes n variables all with file scope. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              1 Reply Last reply
              0
              • M Manfred Staiger

                I don't know if you are completely right. Certainly, you get an errormessage on an assignment in a header file. But in this case I wouldn't be amazed if the whole "extern"-statement is a problem, because his globalVar isn't "extern" in this case. I didn't try out. Anyhow, I wouldn't place an "extern"-statement in the stdafx.h, even because it is included everywhere else. But that's a question of the point of view. MS

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                There is nothing wrong with declaring a variable extern and then defining it in the same compilation module. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                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