首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于个g++的有关问题

2012-03-11 
关于个g++的问题定义了一个类模板,把接口和实现分别放在两个文件中,分别是chain.h和chain.cpp,主函数在nl.

关于个g++的问题
定义了一个类模板,把接口和实现分别放在两个文件中,分别是chain.h和chain.cpp,主函数在nl.cpp中。
chain.cpp和nl.cpp中都已经包含chain.h,不知道为什么连接时出现下面的错误:
[root@hust nl]# g++ -c chain.cpp
[root@hust nl]# ls
chain.cpp chain.h chain.o nl.cpp
[root@hust nl]# g++ -c nl.cpp
[root@hust nl]# g++ -o main chain.o nl.o
nl.o: In function `main':
nl.cpp:(.text+0xb7): undefined reference to `chain<int>::cn_insert(int, int const&)'
nl.cpp:(.text+0xd8): undefined reference to `chain<int>::cn_insert(int, int const&)'
nl.cpp:(.text+0xeb): undefined reference to `chain<int>::cn_output(std::basic_ostream<char, std::char_traits<char> >&) const'
nl.cpp:(.text+0x12f): undefined reference to `chain<int>::cn_insert(int, int const&)'
nl.cpp:(.text+0x153): undefined reference to `chain<int>::cn_output(std::basic_ostream<char, std::char_traits<char> >&) const'
nl.cpp:(.text+0x179): undefined reference to `chain<int>::~chain()'
nl.cpp:(.text+0x18c): undefined reference to `chain<int>::~chain()'
nl.cpp:(.text+0x19c): undefined reference to `chain<int>::~chain()'
nl.cpp:(.text+0x1af): undefined reference to `chain<int>::~chain()'
collect2: ld 返回 1
[root@hust nl]# 

把类模板的接口和函数的定义放在一个文件中就没有问题,对模板不是很了解,

[解决办法]
g++不支持模板分离编译,你还是写到一个文件中吧
[解决办法]
你先把头文件放在放进去编译生成.o

如果最后连接.o还是连接出错的话

应该是你使用的版本不支持模板分离编译

只能使用包含编译,或者使用export再试试

热点排行
Bad Request.