Suppose I have a class template A
, and non-template class B
inheriting from A
. While A
compiles OK, compiling B
triggers linker errors. Specifically,
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;
}
Compiling testA.cpp
as follows is successful:
$ g++ -std=c++11 testA.cpp
<- OK
Next up is non-template class B
inheriting from 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;
}
Compilation of testB.cpp
seems to go ok, but the linker is not a happy camper:
Attempt 1
$ g++ -std=c++11 testB.cpp
Undefined references to B(), and ~B()
collect2: error: ld returned 1 exit status
Attempt 2
$ g++ -std=c++11 testB.cpp B.cpp
Undefined reference to B.foo()
collect2: error: ld returned 1 exit status
Any help/ideas are greatly appreciated. Mr. ld
kept me up most of the night, and is threatening my sanity.
Edit
Thank you Mike Seymour! This minimal example was not a true rendition of the real code, as indeed a qualifier was missing in the implementation, and was the wrench in the gear.