By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Simple question about templates

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.