I have a C++ using a class specified on separate .hpp/.cpp files. This class has the particularity of having a templated member function that takes a const std::vector
as argument, and here is the root of the problem.
myclass.hpp
:
class myclass {
//...
public:
myclass();
template <typename _t> int myfunction(const std::vector<_t> *values);
//...
}
myclass.cpp
:
#include "myclass.hpp"
//...
template <typename _t> int myclass::myfunction(const std::vector<_t> *values){
// do stuff
return 0;
}
and my main.cpp
:
#include "myclass.hpp"
int main(int argc, char const *argv[]){
myclass foo;
std::vector<int> bar(5,100);
foo.myfunction(bar);
return 0;
}
However, when I try to compile all this with g++ main.cpp myclass.cpp -o main -I /usr/include
I get the error:
undefined reference to `int myclass::myfunction<int>(std::vector<int, std::allocator<int> > const*)'
which is strange since the syntax seems to be correct (and it passes g++ check). The problem is not with how I am building the code or the file setup, since I am able to compile the code if I comment out the templated function.