Simple question about templates

N

neojohn75

Hello,
I have a class with a number of functions like this one.

typedef unsigned char Pixel;
.....
template <class T>
void Image2D<T>::Char2Pixel(Pixel * c, T *p, int dimx, int dimy){
int i,j,k=0;

for(i=0;i<dimy;i++)
for(j=0;j<dimx;j++,k++){
p[k] = (T) c[k];
}
}

I can compile the class successfully. Now, I'm trying to generate a
shared library, but during the linking process I get the following
errors for all the my functions.

.....
libexample.so: undefined reference to `Image2D<unsigned
char>::Char2Pixel(unsigned char*, unsigned char*, int, int)'
libexample.so: undefined reference to
`Image2D<short>::Char2Pixel(unsigned char*, short*, int, int)'
libexample.so: undefined reference to
`Image2D<float>::Char2Pixel(unsigned char*, float*, int, int)'
.....

Any ideas what I'm doing wrong with my templates?

Thanks!
 
J

Jerry Coffin

Hello,
I have a class with a number of functions like this one.

[ template code elided ... ]
I can compile the class successfully. Now, I'm trying to generate a
shared library, but during the linking process I get the following
errors for all the my functions.

Most compilers require that the source code to a template is visible
anywhere that template is used. A few support support export, so you
can put a template into a separate object file. I doubt any supports
putting one into a shared library though.
 
T

Thomas Tutone

neojohn75 said:
I have a class with a number of functions like this one.

typedef unsigned char Pixel;
....
template <class T>
void Image2D<T>::Char2Pixel(Pixel * c, T *p, int dimx, int dimy){
int i,j,k=0;

for(i=0;i<dimy;i++)
for(j=0;j<dimx;j++,k++){
p[k] = (T) c[k];
}
}

I can compile the class successfully. Now, I'm trying to generate a
shared library, but during the linking process I get the following
errors for all the my functions.

....
libexample.so: undefined reference to `Image2D<unsigned
char>::Char2Pixel(unsigned char*, unsigned char*, int, int)'
libexample.so: undefined reference to
`Image2D<short>::Char2Pixel(unsigned char*, short*, int, int)'
libexample.so: undefined reference to
`Image2D<float>::Char2Pixel(unsigned char*, float*, int, int)'
....

Any ideas what I'm doing wrong with my templates?

It's in the FAQ:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top