How to inherit from a virtual template class in this code:
class Base {
public:
virtual std::string Foo() = 0;
virtual std::string Bar() = 0;
};
template <typename T>
class Derived : public Base {
public:
Derived(const T& data) : data_(data) { }
virtual std::string Foo();
virtual std::string Bar();
T data() {
return data_;
}
private:
T data_;
};
typedef Derived<std::string> DStr;
typedef Derived<int> DInt;
template<typename T>
std::string Derived<T>::Foo() { ... }
template<typename T>
std::string Derived<T>::Bar() { ... }
When I try to use DStr or DInt, the linker complains that there are unresolved external characters that are Derived<std::string>::Foo()both Derived<std::string>::Bar()and the same for Derived<int>.
Am I missing something in my code?
EDIT: Thanks to everyone. Now it is pretty clear.
source
share