The following code produces an "Undefined symbols" error. How can it be modified to work properly?
I'm trying to make an interface which allows printing data regardless of its type. The derived classes determine the type of data to be printed.
template<typename T>
class Base {
T data_;
public:
friend std::ostream &operator<<(std::ostream &, const Base<T> &);
};
template<typename T>
std::ostream &operator<<(std::ostream &os, const Base<T> &) {
// ...
return os;
}
class Derived : public Base<int> {
};
Derived d;
std::cout << d << '\n';