# [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 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?
[code]#ifndef MATH_H
#define MATH_H
namespace Math {
const float PI = 3.141592654f;
const float INVPI = 0.318309886f;
}
#endif
[/code]

##### 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
[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 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.

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

##### 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 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.

##### 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628294
• Total Posts
2981882

• 9
• 9
• 11
• 10
• 10