LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 827|回复: 3

const 怪问题!翻过书了,依然不明不白(经典?)

[复制链接]
发表于 2006-12-6 18:32:10 | 显示全部楼层 |阅读模式
//test.c
int a=3;

//Main.c
#include <iostream>
using std::cout;
using std::endl;
extern int a;
main()
{
    cout<<a<<endl;
}

用    g++  -o  main  main.c  test.c    编译成功,运行输出 3,结果正确

但是,问题就在这里:若在 test.c 中把 int a=3 改为 const int a=3,
Main.c中用 extern const int a;
编译失败!
/tmp/ccRQXq7q.o: In function `main':Main.cpp.text+0x84):对‘a’未定义的引用
collect2: ld 返回 1

我是C++新手,书上没有提到 const 限制的 对象不能被其他文件的函数使用,故求救。
发表于 2006-12-6 19:35:49 | 显示全部楼层
const 变量必须初始化吧
回复 支持 反对

使用道具 举报

发表于 2006-12-6 23:58:18 | 显示全部楼层
"The cpp programming language" says: By defalut, consts and typedefs have internal linkage.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-7 20:22:59 | 显示全部楼层
Thanks so much.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表