Public Group

[C++] Variable in namespace

This topic is 2484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello,

I'm new to namespaces and trying to define PI and 1/PI values inside namespace like this:#pragma once namespace Math { const float PI = 3.141592654f; const float INVPI = 0.318309886f;};

However I get redefinition error when I include this header more than once. What's the proper way to achieve this?

Thank you.

Share on other sites
Show us the code that exactly reproduces error. Also paste exact error message.

Share on other sites
I thought that #pragma once would prevent this kind of error from happening. How about standard include guards?
#ifndef MATH_H #define MATH_H namespace Math { const float PI = 3.141592654f; const float INVPI = 0.318309886f; } #endif 

Share on other sites
I prefer the standard guards, but pragma once works too.

From what you have posted I don't see the problem, it must be with the way you include the file :S

Share on other sites

Hello,

I'm new to namespaces and trying to define PI and 1/PI values inside namespace like this:#pragma once namespace Math { const float PI = 3.141592654f; const float INVPI = 0.318309886f;};

However I get redefinition error when I include this header more than once. What's the proper way to achieve this?

Thank you.

The joy of C++ is that compiler error messages can be incredibly misleading. Often you'll find that the actual error has nothing to do with what the compiler is telling you is the problem.

Share on other sites

Maybe "Math::PI" is already being included from a different source?

On a side note, I'm pretty sure that pragmas are non-standard (i.e., compiler specific). So whether or not one will work depends on the compiler being used. The reason for the include guard method is that it's portable across compilers because it uses only standard language features.

Edited by vreality

Share on other sites
Aren't you trying to declare and initiailize a global variable on a header?

Share on other sites

Aren't you trying to declare and initiailize a global variable on a header?

Not quite. If it was truly global, he'd have to declare it [font="'Courier New"]extern[/font]. There's nothing wrong with declaring a constant in a header file. The constant variable will not be able to be referenced by address across multiple source files, though each source file will get its own "copy" of a const float variable, all of which will have the same value (but not the same address!).

Anyway, my \$ is on either[font="'Courier New"] #pragma once[/font] not working with your compiler (like VReality said), or some other error not really related to what it is you're asking.

Share on other sites
#pragma once works on Visual Studio,LLVM and GCC, so unless the compiler is seriously out dated I doubt it is the #pragma causing the problem.

What is the exact error?

Share on other sites
Well the error listed in the subtitle or whatever that crazy region is called is "error C2370: 'Math::PI' : redefinition; different storage clas" which is the MSVC error that shows when a const global variable is redefined.

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631757
• Total Posts
3002126
×