I have a class declared with:
in unit.h
:
class unit {
...
template<typename T>
static
void *marshal(void *buff, const std::string &name, T value);
...
};
in unit.cpp
:
template<typename T>
void *unit::marshal(void *buff, const std::string &name, T value) {
...
return buff;
}
Somewhere else I try to call it:
...
unsigned char buffer[100], *end = nullptr;
end = (unsigned char *) unit::marshal(buffer, "Chimes1_Volume", 222);
...
compiler is happy, but linker bombs with:
undefined reference to `void* AW::unit::marshal<int>(void*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
What am I doing wrong?
Note: previously I had a friend
function, instead of the static
method, but I got the same problem.
UPDATE:
If I move the implementation in the header file, inside class declaration, it works, but I need to understand what's wrong with the split .h
/.cpp
implementation.
working unit.h
contains:
class unit {
...
template<typename T>
static
void *marshal(void *buff, const std::string &name, T value); {
...
return buff;
}
...
};
body was copy&paste, of course.
I am pretty sure I copied the definitions right, but...