template-fn implemented in different object does not link

Discussion in 'C++' started by reppisch, Oct 12, 2006.

  1. reppisch

    reppisch Guest

    Hi Ng,

    i tried to instantiate a template class in a different compilation unit
    than it is implemented. It seems while linking it together the linker
    does not call the compiler to generate the missing functions.

    Doing it all in one object seems no problem.


    How to reproduce:

    --------------
    scratchpad.hpp
    --------------
    template < typename T >
    class TemplatedBase
    {
    public:
    T member;
    int foo(const T arg);
    };

    --------------
    scratchmod.cpp
    --------------
    #include "scratchpad.hpp"

    template < typename T >
    int TemplatedBase<T>::foo (const T arg)
    {
    return 1;
    }

    --------------
    scratchpad.cpp
    --------------
    #include "scratchpad.hpp"

    int main()
    {
    char c = 42;
    TemplatedBase<char> p;
    p.foo(c);
    }

    --------------

    g++ scratchpad.cpp scratchmod.cpp

    /cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
    reference to `TemplatedBase<char>::foo(char)'

    Is it a bug or a feature? And how to do it right?



    Regards,

    Michael
     
    reppisch, Oct 12, 2006
    #1
    1. Advertising

  2. reppisch

    Sumit Rajan Guest

    reppisch wrote:
    > Hi Ng,
    >
    > i tried to instantiate a template class in a different compilation unit
    > than it is implemented. It seems while linking it together the linker
    > does not call the compiler to generate the missing functions.
    >
    > Doing it all in one object seems no problem.
    >
    >
    > How to reproduce:
    >
    > --------------
    > scratchpad.hpp
    > --------------
    > template < typename T >
    > class TemplatedBase
    > {
    > public:
    > T member;
    > int foo(const T arg);
    > };
    >
    > --------------
    > scratchmod.cpp
    > --------------
    > #include "scratchpad.hpp"
    >
    > template < typename T >
    > int TemplatedBase<T>::foo (const T arg)
    > {
    > return 1;
    > }
    >
    > --------------
    > scratchpad.cpp
    > --------------
    > #include "scratchpad.hpp"
    >
    > int main()
    > {
    > char c = 42;
    > TemplatedBase<char> p;
    > p.foo(c);
    > }
    >
    > --------------
    >
    > g++ scratchpad.cpp scratchmod.cpp
    >
    > /cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
    > reference to `TemplatedBase<char>::foo(char)'
    >
    > Is it a bug or a feature? And how to do it right?



    See:
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.12

    Regards,
    Sumit.
     
    Sumit Rajan, Oct 12, 2006
    #2
    1. Advertising

  3. reppisch

    reppisch Guest

    reppisch, Oct 12, 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. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    891
    Kevin Spencer
    May 17, 2005
  2. Replies:
    1
    Views:
    337
    John Harrison
    Feb 21, 2007
  3. Davy
    Replies:
    2
    Views:
    423
    Terry Reedy
    Nov 10, 2007
  4. Jonas
    Replies:
    0
    Views:
    169
    Jonas
    Sep 23, 2003
  5. inetquestion
    Replies:
    0
    Views:
    157
    inetquestion
    May 6, 2009
Loading...

Share This Page