所有
我制作了一组c ++代码如下
然后,我成功地用g ++将每个.cpp
编译为.o
但是,我收到了链接错误
Undefined symbols for architecture x86_64:
"derive1<3>::derive1(int)", referenced from:
derive2::derive2(int)in derive2.o
derive2::derive2(int)in derive2.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
你可以告诉我发生了什么事吗?如果我将所有代码片段绑定在一个文件中,我没有任何错误,并且它完全按照我的意图运行。我假设我应该在某处引入extern关键字,但我不知道该怎么做。
我的编译器是
i686-apple-darwin11-llvm-g ++ - 4.2(GCC)4.2.1(基于Apple Inc.) build 5658)(LLVM build 2336.11.00)
“base.hpp”
class base {
public:
virtual int hoge( int num ) = 0;
};
“derive1.hpp”
#include "base.hpp"
#include <iostream>
template <int N>
class derive1 : public base {
public:
explicit derive1( int n );
virtual int hoge( int num ) {
std::cout << "num = " << num << std::endl
<< "N = " << N << std::endl;
return N;
}
};
“derive1.cpp”
#include "derive1.hpp"
template <int N>
derive1<N>::derive1 ( int n ) {
std::cout << "This is the constructer of derive1" << std::endl
<< "N = " << N << ", n = " << n << std::endl;
}
“derive2.hpp”
#include "derive1.hpp"
#include <iostream>
class derive2 : public derive1<3> {
public:
explicit derive2( int n );
};
“derive2.cpp”
#include "derive2.hpp"
derive2::derive2( int n ) : derive1<3>( n ) {
std::cout << "This is the constructer of derive2" << std::endl
<< "n = " << n << std::endl;
}
“derive_test.cpp”
#include<iostream>
#include "derive2.hpp"
int main() {
derive2 test(3);
std::cout << "return value = "
<< test.hoge( 5 )
<< std::endl;
return 0;
}
答案 0 :(得分:1)
您不应该为模板类拆分.h和.cpp文件的定义和实现。
What is an undefined reference/unresolved external symbol error and how do I fix it?
答案 1 :(得分:1)
不幸的是,大多数编译器都要求模板函数的实现(在这种情况下template <int N> derive1<N>::derive1 ( int n )
可以使用它来编译单元。将它移动到“derive1.hpp”。
答案 2 :(得分:1)
简单的答案是,您必须将所有模板化代码(derive1::derive1(int)
等)放入头文件中,而不是放入自己的翻译单元中。编译器在尝试实例化模板时需要查看完整定义。当然,你可以在*.cpp
文件中有模板的定义,但是你必须明确地规范它,并在头文件中声明这种特殊化。