在头不工作常量变量 [英] constant variables not working in header

查看:152
本文介绍了在头不工作常量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义我不断的可变因素在我的头像这样...

if I define my constant varibles in my header like this...

extern const double PI = 3.1415926535;
extern const double PI_under_180 = 180.0f / PI;
extern const double PI_over_180 = PI/180.0f;

我收到以下错误

1>MyDirectX.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj

但如果我从头部取下这些常数,并把它们所包含这样的头文件中...

but If I remove those constants from the header and put them in the document that is including the header like this...

const double PI = 3.1415926535;
const double PI_under_180 = 180.0f / PI;
const double PI_over_180 = PI/180.0f;

它的工作原理

有没有人有知道我可能是做错了?

Does anyone have Idea what I might be doing wrong ??

感谢

推荐答案

的问题是,你的确定的在头文件外部链接的对象。果然,一旦包含头文件分成多个翻译单元,你会得到一个具有外部链接相同的对象,这是一个错误的多个定义。

The problem is that you define objects with external linkage in header file. Expectedly, once you include that header file into multiple translation units, you'll get multiple definitions of the same object with external linkage, which is an error.

正确的方式来做到这取决于你的意图。

The proper way to do it depends on your intent.

(1)您可以把你定义成头文件,但要确保他们的内部的联动。

(1) You can put your definitions into the header file, but make sure that they have internal linkage.

在C,它需要一个明确的静态

In C that would require an explicit static

static const double PI = 3.1415926535; 
static const double PI_under_180 = 180.0f / PI; 
static const double PI_over_180 = PI/180.0f; 

在C ++ 静态是可选的(因为在C ++ 常量对象在默认情况下内部链接)

In C++ static is optional (because in C++ const objects have internal linkage by default)

const double PI = 3.1415926535; 
const double PI_under_180 = 180.0f / PI; 
const double PI_over_180 = PI/180.0f; 

(2)或你可以把单纯的非定义声明的成头文件,并把定义的为一个(且只有一个)实现文件

(2) Or you can put mere non-defining declarations into the header file and put the definitions into one (and only one) implementation file

在声明中的的文件中必须包含一个明确的的extern 没有初始化

The declarations in the header file must include an explicit extern and no initializer

extern const double PI; 
extern const double PI_under_180; 
extern const double PI_over_180; 

在定义中的一个的实施的文件应该如下:

const double PI = 3.1415926535; 
const double PI_under_180 = 180.0f / PI; 
const double PI_over_180 = PI/180.0f; 

(显式的的extern 中的定义是可选的,如果上述声明precede的定义)。

(explicit extern in the definitions is optional, if the above declarations precede the definitions).

哪种方法你会选择取决于你的意图。

Which method you will choose depends on your intent.

第一种方法可以更容易的编译器优化code,因为它可以看到在每个转换单元的恒定的实际值。但在同一时间概念你在每一个翻译单元分离的,独立常量对象。例如,&放大器; PI 将评估在每个转换单元不同的地址

The first method makes it easier for the compiler to optimize the code, since it can see the actual value of the constant in each translation unit. But at the same time conceptually you get separate, independent constant objects in every translation unit. For example, &PI will evaluate to a different address in each translation unit.

第二个方法创建一个真正的全球的常数,即由整个程序共享独特的恒定对象。例如,&放大器; PI 将评估在每个翻译单元相同的地址。但在这种情况下,编译器只能看到在一个和实际值仅一个转换单元,这可能会妨碍优化

The second method creates truly global constants, i.e. unique constant objects that are shared by the entire program. For example, &PI will evaluate to the same address in each translation unit. But in this case the compiler can only see the actual values in one and only one translation unit, which might impede optimizations.

这篇关于在头不工作常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆