Question

I'm trying to overload a "display" method as follows:

template <typename T> void imShow(T* img, int ImgW, int ImgH);
template <typename T1, typename T2> void imShow(T1* img1, T2* img2, int ImgW, int ImgH);

I am then calling the template with unsigned char* im1 and char* im2:

imShow(im1, im2, ImgW, ImgH);

This compiles fine, but i get a link error "unresolved external symbol" for:

imShow<unsigned char,char>(unsigned char *,char *,int,int)

I don't understand what I did wrong!

Was it helpful?

Solution

You probably forgot to define your template function properly. Where are the definitions? I don't see any in your post.

OTHER TIPS

You need to define that template in the header file if your compiler doesn't have the "export" template feature (only compilers based on the EDG frontend have, which GCC and MSVC do not). You can alternatively explicitly instantiate the function template in the .cpp file (if you placed its definition there):

template void imShow(unsigned char* img1, char* img2, int ImgW, int ImgH);

But as soon as you pass another pair of types you haven't explicitly instantiated like that, it again fails to link. So you need to put the function template's definition into the header, so the compiler sees it when calling the function, and instantiates a copy of the function itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top