Simple question about templates

Discussion in 'C++' started by neojohn75, Jul 4, 2006.

  1. neojohn75

    neojohn75 Guest

    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!
     
    neojohn75, Jul 4, 2006
    #1
    1. Advertising

  2. neojohn75

    Jerry Coffin Guest

    In article <>,
    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.
     
    Jerry Coffin, Jul 4, 2006
    #2
    1. Advertising

  3. 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-lite/templates.html#faq-35.13

    Best regards,

    Tom
     
    Thomas Tutone, Jul 4, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    604
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    386
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    479
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    369
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    812
    recover
    Jul 25, 2006
Loading...

Share This Page