クラス templateと、から継承するA
非テンプレート クラスがあるとします。コンパイルは問題ありませんが、コンパイルによってリンカエラーが発生します。具体的には、B
A
A
B
A.hpp
#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED
template <typename T>
class A {
public:
A();
A(A<T>&& );
virtual ~A();
/* ... */
protected:
T x;
};
#include A.tpp
#endif
A.tpp
#include "A.hpp"
template <typename T>
A<T>::A() { ... }
template <typename T>
A<T>::A(A<T>&& other)
: x(std::move(other.x)) { }
template <typename T>
A<T>::~A() { ... }
testA.cpp
#include "A.hpp"
int main() {
A<std::string> a;
/* ... */
return 0;
}
次のようにコンパイルtestA.cpp
すると成功します。
$ g++ -std=c++11 testA.cpp
<-OK
class B
次はから継承する非テンプレートA
です:
B.hpp
#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED
#include "A.hpp"
class B
: public A<std::string> {
public:
B();
virtual ~B();
static A<std::string> foo();
};
#endif
B.cpp
#include "B.hpp"
B::B()
: A(std::move(foo())) { }
B::~B() { }
A<std::string> B::foo() { ... }
testB.cpp
#include "B.hpp"
int main() {
B b;
/* ... */
return 0;
}
のコンパイルはtestB.cpp
うまくいっているようですが、リンカーは幸せなキャンパーではありません。
試行 1
$ g++ -std=c++11 testB.cpp
Undefined references to B(), and ~B()
collect2: error: ld returned 1 exit status
試行 2
$ g++ -std=c++11 testB.cpp B.cpp
Undefined reference to B.foo()
collect2: error: ld returned 1 exit status
どんな助け/アイデアも大歓迎です。氏ld
はほとんど夜通し私を起こして、私の正気を脅かしています。
編集
ありがとうマイク・シーモア!この最小限の例は、実際のコードを正確に表現したものではありませんでした。実際、実装には修飾子がなく、ギアのレンチでした。