I'm getting linker errors from clang++ when trying to use a templated operator I wrote.
The errors are:
main.cpp:(.text+0xfe): undefined reference to `operator<<(std::ostream&, schematic<130ul, 128ul, 130ul> const&)'
main.cpp:(.text+0x28c): undefined reference to `operator<<(std::ostream&, schematic<130ul, 1ul, 130ul> const&)'
Now before I continue or you flag this as duplicate: I know you can't put a template definition in a separate .cpp file, and I didn't do that. The operator's definition is in a separate header file that gets included into the one with it's declaration, as described in this answer. Since it's still giving me linker errors, I'm asking here.
Code
main.cpp
// includes
int main(int argc, char const* argv[])
{
// ...
switch (/* call */)
{
case /* one */:
fout << mapArtFlat(/* ctor params */).exportScematic();
break;
case /* another */:
fout << mapArtStaircase(/* ctor params */).exportSchematic();
break;
}
return 0;
}
schematic.h
#ifndef SCHEMATIC_H
#define SCHEMATIC_H
// includes
template <std::size_t W, std::size_t H, std::size_t L>
class schematic
{
friend std::ostream& operator<<(std::ostream&, const schematic<W,H,L>&);
// class things
};
// output operator
template <std::size_t W, std::size_t H, std::size_t L>
std::ostream& operator<<(std::ostream&, const schematic<W,H,L>&);
// ...
#include "schematic_cpp.h"
#endif
schematic_cpp.h
// ...
// output operator
template <std::size_t W, std::size_t H, std::size_t L>
std::ostream& operator<<(std::ostream& lhs, const schematic<W,H,L>& rhs)
{
// lot of code involving external libraries and processing data from the object
}
// ...
Things I tried
- Commenting out the
operator<<
declaration in schematic.h - Making the
operator<<
inline