这是VS 2005的C ++编译器的错误吗? [英] Is this a bug of the C++ compiler of VS 2005?

查看:62
本文介绍了这是VS 2005的C ++编译器的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先看一个例子:


hdr.h

--------------


struct X

{

static const int m = 9;

};


=====================

source1.cpp

---------------


#include" hdr.h"


const int X :: m;


=====================


main .cpp

---------------


#include" hdr.h"


int main()

{}


================= ====


链接错误:

source1.obj:错误LNK2005:" public:static int const X :: m" (?

m @ X @@ 2HB)已在main.obj中定义

根据C ++标准9.4.2 / 4

"如果一个静态数据成员是const整数或const枚举

类型,它在类定义中的

声明可以指定一个常量初始化器



应该是一个整数常量表达式。在这种情况下,成员

可以

出现在其范围内的整数常量表达式中。会员

如果在程序中使用,仍然会在命名空间范围内定义




命名空间范围定义不应包含初始化程序。


从上述规则判断,我的代码是正确的。但是,如果我用
评论语句:const int X :: m;一切都好。为什么?

解决方案

Lighter写道:


查看示例at第一:


hdr.h

--------------


struct X

{

static const int m = 9;

};


= ====================

source1.cpp

------- --------


#include" hdr.h"


const int X :: m;


=====================

main.cpp

---------------


#include" hdr.h"


int main()

{}


=====================


链接错误:

source1.obj:错误LNK2005:" public:static int const X :: m" (?

m @ X @@ 2HB)已在main.obj中定义


根据C ++标准9.4.2 / 4

"如果一个静态数据成员是const整数或const枚举

类型,它在类定义中的

声明可以指定一个常量初始化器



应为整数常量表达式。在这种情况下,成员

可以

出现在其范围内的整数常量表达式中。会员

如果在程序中使用,仍然会在命名空间范围内定义




命名空间范围定义不应包含初始化程序。


从上述规则判断,我的代码是正确的。但是,如果我用
评论语句:const int X :: m;一切都好。为什么?



看起来像链接器中的缺陷。发布到VC ++新闻组和/或

向Microsoft提交错误报告。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


这是标准的C ++行为。


只需删除= 9即可。从你的结构定义开始,它将以你期望的方式运行



6月6日上午9:47,Lighter< cqu ... @ gmail.comwrote :


首先看一个例子:


hdr.h

---- ----------


struct X

{

static const int m = 9;


};


=====================
< br $> b $ b source1.cpp

---------------


#include" hdr .h"


const int X :: m;


================ =====


main.cpp

---------------

#include" hdr.h"


int main()

{}


=====================

链接错误:

source1.obj:错误LNK2005:public:static int const X :: m (?

m @ X @@ 2HB)已在main.obj中定义


根据C ++标准9.4.2 / 4

"如果一个静态数据成员是const整数或const枚举

类型,它在类定义中的

声明可以指定一个常量初始化器



应为整数常量表达式。在这种情况下,成员

可以

出现在其范围内的整数常量表达式中。会员

如果在程序中使用,仍然会在命名空间范围内定义




命名空间范围定义不应包含初始化程序。


从上述规则判断,我的代码是正确的。但是,如果我用
评论语句:const int X :: m;一切都好。为什么?



witkamp写道:


这是标准的C ++行为。 />

只需删除= 9即可。从你的结构定义中它将以你期望的方式运行




lol。当然,删除一个程序解决了问题:)


但是你读过这条消息了吗?如果他想要在类定义中初始化他的

const static int成员,为什么要删除= 9?

他也引用了标准,所以,如果你不这样做同意一些事情,

请提供解释。


问候,


Zeppe


See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;
};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj
According to the C++ standard 9.4.2/4
"If a static data member is of const integral or const enumeration
type, its
declaration in the class definition can specify a constant-initializer
which
shall be an integral constant expression. In that case, the member
can
appear in integral constant expressions within its scope. The member
shall
still be defined in a namespace scope if it is used in the program and
the
namespace scope definition shall not contain an initializer."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?

解决方案

Lighter wrote:

See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;
};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj
According to the C++ standard 9.4.2/4
"If a static data member is of const integral or const enumeration
type, its
declaration in the class definition can specify a constant-initializer
which
shall be an integral constant expression. In that case, the member
can
appear in integral constant expressions within its scope. The member
shall
still be defined in a namespace scope if it is used in the program and
the
namespace scope definition shall not contain an initializer."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?

Looks like a defect in their linker. Post to the VC++ newsgroup and/or
submit a bug report to Microsoft.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


This is standard C++ behavior.

Just remove the "= 9" from your struct definition and it will work the
way you would expect.
On Jun 6, 9:47 am, Lighter <cqu...@gmail.comwrote:

See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;

};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj

According to the C++ standard 9.4.2/4
"If a static data member is of const integral or const enumeration
type, its
declaration in the class definition can specify a constant-initializer
which
shall be an integral constant expression. In that case, the member
can
appear in integral constant expressions within its scope. The member
shall
still be defined in a namespace scope if it is used in the program and
the
namespace scope definition shall not contain an initializer."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?



witkamp wrote:

This is standard C++ behavior.

Just remove the "= 9" from your struct definition and it will work the
way you would expect.

lol. Of course, removing a piece the program solves the problem :)

But have you read the message? Why should him remove =9 if he wants his
const static int member to be initialized in the class definition?
He also cited the standard, so, if you do not agree with something,
please provide an explanation.

Regards,

Zeppe


这篇关于这是VS 2005的C ++编译器的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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