Jump to content
  • Advertisement
Sign in to follow this  
Ripiz

[C++] Variable in namespace

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Of course if your compiler didn't recognize your pragma directive, it would be complaining about that.

Edited by vreality

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!