468,458 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Simple question about templates

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!

Jul 4 '06 #1
2 1024
In article <11*********************@h44g2000cwa.googlegroups. com>,
ne*******@yahoo.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 4 '06 #2
neojohn75 wrote:
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-lit...html#faq-35.13

Best regards,

Tom

Jul 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by chris | last post: by
9 posts views Thread by Mike King | last post: by
1 post views Thread by Scott | last post: by
5 posts views Thread by Support | last post: by
75 posts views Thread by Steven T. Hatton | last post: by
17 posts views Thread by JohnQ | last post: by
3 posts views Thread by stdlib99 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.