const相关

wshcdr 2005-09-22 02:48:15
我建立一个工程,内含三个文件
//////////////////////
extern.cpp

const int nNum = 246;
///////////////////
main.h

extern const int nNum;

//////////////////
main.cpp

#include "Main.h"

void main()
{
//cout << nNum << endl;
int a = nNum;
}

编译居然不过,报错如下
Main.obj : error LNK2001: unresolved external symbol "int const nNum" (?nNum@@3HB)

VC6+SP5

难道const居然不支持 extern const?
...全文
121 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜牛 2005-09-22
  • 打赏
  • 举报
回复
同意“晚九朝五”,.h文件中声明:
extern int nNum;
.cpp文件中定义:
int nNum = 246;
goodboyws 2005-09-22
  • 打赏
  • 举报
回复
其实i_noname解释的更清楚,惭愧
wshcdr 2005-09-22
  • 打赏
  • 举报
回复
深夜不眠者是正解

i_noname(晚九朝五)
你这个用法已经违背我的本意了
我是外部引用extern.cpp中的const变量
i_noname 2005-09-22
  • 打赏
  • 举报
回复
比较合规范的写法应该是这样:

///////////////////
main.h

extern const int nNum; //定义nNum为外部const变量
//////////////////////
extern.cpp

include "main.h"
const int nNum = 246; //只有一个cpp可以初始化nNum,这时不能用extern关键字

//////////////////
main.cpp

#include "Main.h"
void main()
{
//cout << nNum << endl;
int a = nNum;
}
goodboyws 2005-09-22
  • 打赏
  • 举报
回复
PRB: Constant String Is Eliminated by the C++ Compiler

Q87634


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft C/C++ Compiler (CL.EXE)
Microsoft C/C++ for MS-DOS, version 7.0
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 2.1, 4.0, 4.1, 4.2, 5.0, 6.0

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


SYMPTOMS
The Microsoft C/C++ versions 7.0 and later compilers eliminate unreferenced constant strings. This happens only with C++ source, not C source. The string is declared in the source file, but is not included in the resulting object module.



CAUSE
In C++, const variables have internal linkage. This is different from the way that C handles const variables.

In this case, because the C++ compiler can process only one source file at a time, the string is eliminated if it is not referenced in the file in which it is declared, even if it is referenced from another file.



RESOLUTION
The solution is to declare the const string as "extern". This provides external linkage, telling the compiler that the string may be used in another module, and not to assume that it is an unreferenced symbol; for example


const char sz[] = "\"the string\"";
would be optimized away in a C++ module, but the following string would not:

extern const char sz[] = "\"the string"\";



MORE INFORMATION
While this behavior is not a bug in the compiler, the effects of this optimization may cause unexpected results. If a string is declared and initialized in one module, and referenced only in another module, the string will be eliminated from the first module. This means that the other file will not have access to the information used in the initialization of the string.

For more information on internal and external linkage, see section 2.4 of the ""C++ Language Reference"" supplied with C/C++ version 7.0 and Visual C++, or refer to the online help supplied with Visual C++.

Additional query words: 8.00 8.00c 9.00 9.10 L2029

Keywords : kbCompiler kbCPPonly kbVC100 kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC410 kbVC420 kbVC500 kbVC600
Issue type : kbprb
Technology : kbvc

goodboyws 2005-09-22
  • 打赏
  • 举报
回复
const int nNum = 246;
改为
extern const int nNum = 246;
i_noname 2005-09-22
  • 打赏
  • 举报
回复
const是内部链接性的,如果要在另一个cpp中使用,必须用
extern const来定义从而使其具有外部链接性。
DentistryDoctor 2005-09-22
  • 打赏
  • 举报
回复
应该是extern.cpp没有参与编译与链接。
快乐鹦鹉 2005-09-22
  • 打赏
  • 举报
回复
光有extern const int nNum;不行啊。
main.cpp中要定义:
const int nNum;
i_noname 2005-09-22
  • 打赏
  • 举报
回复
//////////////////////
extern.cpp

extern const int nNum = 246;
///////////////////

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧