Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple question about templates

Reply
Thread Tools

Simple question about templates

 
 
neojohn75
Guest
Posts: n/a
 
      07-04-2006
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!

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      07-04-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      07-04-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments