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. question on use of global scope operator

question on use of global scope operator

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
5 Posts 3 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.
  • D Offline
    D Offline
    digwizfox
    wrote on last edited by
    #1

    Why does code like this sometimes result in a compiler error? mpGlobalQueue->mPushQueue(::GEOSIT_IN, NULL, ::SHUTDOWN); GEOSIT_IN and SHUTDOWN are perfectly valid constants that exist in the global name space. I can use :: to identify global functions within classes, which is very helpful when reading code. But many times when I try to identify constants as global, in this way the compiler gives me errors like the following: error C2589: 'constant' : illegal token on right side of '::'

    A B 2 Replies Last reply
    0
    • D digwizfox

      Why does code like this sometimes result in a compiler error? mpGlobalQueue->mPushQueue(::GEOSIT_IN, NULL, ::SHUTDOWN); GEOSIT_IN and SHUTDOWN are perfectly valid constants that exist in the global name space. I can use :: to identify global functions within classes, which is very helpful when reading code. But many times when I try to identify constants as global, in this way the compiler gives me errors like the following: error C2589: 'constant' : illegal token on right side of '::'

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      This is most often caused by a compiler misunderstanding the context. Another reason for it might be a syntax error earlier in the file. Although what you've written right there is correct, the compiler doesn't quite get it. Try using a space between the opening parenthesis and the first constant. Another trick is to encapsulate the constant into parenthesis' of its own. This forces the compiler to first evaluate the constant value, and then supply it as a parameter to the function. In this case, the compiler (perhaps) thinks that GEOSIT_IN is a member of mpGlobalQueue, which I presume to be an object, struct or union. This causes the error. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

      D 1 Reply Last reply
      0
      • A Antti Keskinen

        This is most often caused by a compiler misunderstanding the context. Another reason for it might be a syntax error earlier in the file. Although what you've written right there is correct, the compiler doesn't quite get it. Try using a space between the opening parenthesis and the first constant. Another trick is to encapsulate the constant into parenthesis' of its own. This forces the compiler to first evaluate the constant value, and then supply it as a parameter to the function. In this case, the compiler (perhaps) thinks that GEOSIT_IN is a member of mpGlobalQueue, which I presume to be an object, struct or union. This causes the error. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

        D Offline
        D Offline
        digwizfox
        wrote on last edited by
        #3

        Thanks for the tip. That is what I thought as well, however even after supplying the parenthesis around the constants, the same error was reported by the compiler. The rest of the code compiles fine and only when I remove the usage of the global scope operator does visual studio compile completely.

        1 Reply Last reply
        0
        • D digwizfox

          Why does code like this sometimes result in a compiler error? mpGlobalQueue->mPushQueue(::GEOSIT_IN, NULL, ::SHUTDOWN); GEOSIT_IN and SHUTDOWN are perfectly valid constants that exist in the global name space. I can use :: to identify global functions within classes, which is very helpful when reading code. But many times when I try to identify constants as global, in this way the compiler gives me errors like the following: error C2589: 'constant' : illegal token on right side of '::'

          B Offline
          B Offline
          Bhaskar
          wrote on last edited by
          #4

          you cannot use :: operator for constants defined using #define. It can be used only for declared identifiers. Look the following code #define CONSTANT1 100 const int CONSTANT2 = 100; main() { cout << ::CONSTANT1; // this is an error cout << " " << ::CONSTANT2; // this is OK }

          D 1 Reply Last reply
          0
          • B Bhaskar

            you cannot use :: operator for constants defined using #define. It can be used only for declared identifiers. Look the following code #define CONSTANT1 100 const int CONSTANT2 = 100; main() { cout << ::CONSTANT1; // this is an error cout << " " << ::CONSTANT2; // this is OK }

            D Offline
            D Offline
            digwizfox
            wrote on last edited by
            #5

            Oh. That would be the problem. I did not know that. Thank you. Shawn

            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