0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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!
@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.
Dropping the template seems like the best solution for my use, thanks anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.