In visual c++, I created a static library with two files, myLib.h and myLib.cpp. I also have a console application project with the file testSequence.cpp that references this library. Within myLib.h I have defined a class template<class prec> class sequence which has the function declaration prec *getPrimes(int numToGet) this function is then defined in myLib.cpp. However, when I build testSequence, there is a linking error, and it says error LNK2019: unresolved external symbol "public: int * __thiscall mathLib::sequence<int>::getPrimes(int)" (?getPrimes@?$sequence@H@mathLib@@QAEPAHH@Z) referenced in function "char * __cdecl codeString(char *,char *,bool)" (?codeString@@YAPADPAD0_N@Z) So, yeah, help would be nice.
Add a comment |
1 Answer
Read this for an explanation of the error.
Basically, what you're trying to do cannot be done. The compiler must be able to see the implementation of the class template when it tries to instantiate it for a given template type parameter. You need to move the implementations for all the member functions to the header file.
3 Comments
bathtub
Crap, I was afraid of this. But isn't this a popular enough framework that there would be some sort of work around? Thank you!
Praetorian
@bathtub Afraid not, export was the C++98 solution to this problem, but it was removed from C++11. You might be able to get some limited functionality using C++11's extern templates. You might be able to explicitly instantiate
sequence for some set of types in your cpp file; and then declare the type as an extern template in the source files of the users of your library. No idea whether this will work.bathtub
Dropping the template seems like the best solution for my use, thanks anyway.