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