• 6049阅读
  • 4回复

关于const和extern的问题 [复制链接]

上一主题 下一主题
离线bienew
 

只看楼主 正序阅读 楼主  发表于: 2007-09-03
— 本帖被 XChinux 执行加亮操作(2008-07-18) —
在a.cpp中,const int index = 0;
a.h中extern const int index;,
在b.cpp中
#include "a.h"
if(index == 0)....
编译报错:...undefined reference to 'index'
把const去掉则就能编译通过,难道qt中不允许extern const变量?
离线steinlee

只看该作者 4楼 发表于: 2008-07-19
This is C style code. It causes confusing for the compiler because Index is const, but not defined locally. It is better to define index as static variable inside a class. Then use class name to access it.
Looking for remote C/C++ and Qt 兼职
离线XChinux

只看该作者 3楼 发表于: 2008-07-18
extern变量的定义放在.cpp中,声明放在.h中。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线bienew

只看该作者 2楼 发表于: 2007-09-04
还是不明白为什么不能放在.h中extern。
因为以前在c环境下编程还有VC中都是这么用的
离线turkeysy

只看该作者 1楼 发表于: 2007-09-03
extern const int index;,
应该写在b.cpp中,表示index是在外部定义的
快速回复
限100 字节
 
上一个 下一个