I have a C++ project with 5 files:
sde.cpp (containing main()), sde_library.cpp, sde_library.hpp, num_vector.cpp and num_vector.hpp. I'm trying to compile them together with the command
g++ sde.cpp sde_library.cpp num_vector.cpp -o sde.out
The main function contains the two lines
int test = 0;
vector<vector<double>> avg_trace = RK_all(num_traces, many_traces, t_interval, y0, dt, test);
And this function is declared in sde_library.hpp as
template<typename eq_params> std::vector<std::vector<double>> RK_all(unsigned int num_traces,
bool many_traces, std::vector<double> t_interval, std::vector<double> y0, double dt, eq_params args);
The implementation is found on sde_library.cpp. However, when compiling I get the following linker error message:
/tmp/ccZkiXkj.o: In function `main':
sde.cpp:(.text.startup+0xd5): undefined reference to `std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > RK_all<int>(unsigned int, bool, std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >, double, int)'
collect2: error: ld returned 1 exit status
I was assuming that the appearance of this error was due to me introducing templates to the function RK_all (before I introduced that it was running correctly); I've seen that "undefined references" appear with templates and it's the first time that I'm using them. However, in the main I'm explicitly stating that eq_params
will be of the int type, so the compiler should understand what this template is. Is that correct? What am I missing, or what else could it be? And how would I fix that?