这对我来说是神秘的。我在Ubuntu使用G ++,这是我的一些代码(与类名更改,但没有别的,因为我仍在使用存根无处不在):未定义的参考与模板类的成员实施
Bob.hpp
template <class A>
class Bob : public Jack<Chris, A>
{
public:
Bob(int x1, int x2, float x3 = 1.0, float x4 = 2.0, float x5 = 3.0) throw(Exception);
virtual ~Bob();
};
Bob.cpp
template <class A>
Bob<A>::Bob(int x1, int x2, float x3, float x4, float x5) throw(Exception)
{
}
template <class A>
Bob<A>::~Bob()
{
}
我:我在这样的另一个文件执行使用这样的:
的main.cpp
int main()
{
Bob<Alice> instance(1, 2);
}
与编译:
g++ -c Bob.cpp -o Bob.o
g++ -c main.cpp -o main.o
g++ -L"libs" -llib main.o Bob.o prog
给我 main.o:在功能main': main.cpp:(.text+0x1fd): undefined reference to
鲍勃::鲍勃(INT,INT,浮动,浮动,浮动)' collect2:ld返回1退出状态
我完全难住了。用g ++连接阶段改变顺序没有区别。编译目标文件不会产生任何问题。为什么在实现构造函数时使用未定义的引用?如果任何人都可以解释这一点,这是非常感谢。
请参阅[为什么我无法将其声明中的模板类的定义与其声明分开并放入.cpp文件中?](http://www.parashift.com/c++-faq-lite/templates.html #faq-35.12)来自C++ FAQ Lite。 – 2010-08-03 19:27:10
@詹姆斯麦克奈利斯:为什么不把它作为答案? :) – 2010-08-03 20:16:31
@Merlyn:我没有时间写出高质量的答案,并且一般认为这是一种糟糕的形式,只是将链接发布到另一个网站作为答案,而不至少总结链接的内容。 – 2010-08-03 20:59:56