Sign in to follow this  
Ripiz

[C++] Variable in namespace

Recommended Posts

Hello,

I'm new to namespaces and trying to define PI and 1/PI values inside namespace like this:[code]#pragma once


namespace Math {
const float PI = 3.141592654f;
const float INVPI = 0.318309886f;};[/code]

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
I thought that #pragma once would prevent this kind of error from happening. How about standard include guards?
[code]#ifndef MATH_H
#define MATH_H
namespace Math {
const float PI = 3.141592654f;
const float INVPI = 0.318309886f;
}
#endif
[/code]

Share this post


Link to post
Share on other sites
[quote name='Ripiz' timestamp='1317322834' post='4867302']
Hello,

I'm new to namespaces and trying to define PI and 1/PI values inside namespace like this:[code]#pragma once


namespace Math {
const float PI = 3.141592654f;
const float INVPI = 0.318309886f;};[/code]

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

Thank you.
[/quote]
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
[i][b]Maybe "Math::PI" is already being included from a different source?[/b][/i]

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 [i][b]that[/b][/i].

Share this post


Link to post
Share on other sites
[quote name='kilah' timestamp='1317335880' post='4867383']
Aren't you trying to declare and initiailize a global variable on a header?
[/quote]
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
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
[quote name='SiCrane' timestamp='1317344507' post='4867420']
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.
[/quote]

Aaahhhh, didnt see that.

Or I should say "Ahhh, didnt se"


I never actually look at that part. My money says you have a either declared that variable twice within your project, or you are using a library that declares it as well. Without seeing the entire project, its almost impossible to diagnose. Do a find in project of "[color=#1C2837][color=#000088][font=CourierNew, monospace][size=2]float[/size][/font][/color][color=#000000][font="CourierNew, monospace"][size="2"] PI" and [/size][/font]did it return anything? [/color][/color]
[font=CourierNew, monospace][size=2]
[/size][/font]
The posted code itself is just fine.[font=CourierNew, monospace][size=2] [/size][/font]

Share this post


Link to post
Share on other sites
Thanks everyone, appears I'm a retard =(
When I was splitting header into several smaller headers (to make it more readable), by mistake I also copied these constants.
Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this