I have a problem with linking the following piece of code. Linker could not find the implementation of friend template function. Any idea why it could not find it?
Declaration
// XY.hpp
namespace xxx
{
template<typename T> class XY;
template<typename T>
std::ostream& operator<<(std::ostream&, const XY<T>&);
template<typename T>
class XY
{
public:
// ... constructors, destructor, getters, setters, etc
friend std::ostream& operator<< <T>(std::ostream&, const XY<T>&);
private:
//...
};
} /* namespace xxx*/
Definition
// XY.cpp
#include "XY.hpp"
namespace xxx
{
template<typename T>
std::ostream& operator<<(std::ostream& os, const XY<T>& rhs)
{
// ...
return os;
}
} /* namespace xxx */
Usage
// main.cpp
#include "XY.hpp"
#include <iostream>
using namespace std;
using namespace xxx;
int main() {
XY<uint16_t> xy;
cout << xy;
return 0;
}
Linker returns error: undefined reference to 'std::ostream& xxx::operator<< <unsigned short>(std::ostream&, xxx::XY<unsigned short> const&)'
Linking with g++ -o "XXX" ./src/XY.o ./src/main.o
Version: gcc.EXE (GCC) 6.3.0