Undefined reference on template class

Discussion in 'C++' started by LaBird, Jan 9, 2004.

  1. LaBird

    LaBird Guest

    Dear all,

    I would like to ask what is the way to separate class
    template definition from declaration in different source
    files? Currently I have the following files (simplified):

    // d.h -- class declaration
    template<typename T>
    class C {
    int x;
    T* p;
    public:
    C();
    T* getp();
    };


    // d.cpp -- class definition
    #include "d.h"

    template <typename T>
    C<T>::C() {
    x = 0;
    p = new T;
    }

    template <typename T>
    T* C<T>::getp() {
    return p;
    }


    // my.cpp -- using class C
    #include "d.h"

    main() {
    C<int> c;
    int *q;

    q = c.getp();
    return 0;
    }

    But when I compile the two .cpp files, I get an error
    from the loader, about undefined reference on the
    c.getp() and constructor. I found that this error
    will not occur if I do not have the template. Why does
    such error occurs for templates, and how to solve it
    (apart from putting the member function definitions
    into d.h)?

    Thanks a lot.

    --
    LaBird (Benny).
     
    LaBird, Jan 9, 2004
    #1
    1. Advertising

  2. LaBird wrote:
    > Dear all,
    >
    > I would like to ask what is the way to separate class
    > template definition from declaration in different source
    > files?


    No. (Your compiler doesn't support the "export" keyword.)

    The nearest hack-around is

    // foo.hpp
    template<typename T> class X{
    void func(const T&);
    }
    #include "foo.cpp"

    // foo.cpp
    template<typename T> void X<T>::func(const T&){}
     
    Jacques Labuschagne, Jan 9, 2004
    #2
    1. Advertising

  3. LaBird

    LaBird Guest

    Dear Jacques,

    Thanks!

    --
    LaBird (Benny).

    "Jacques Labuschagne" <> wrote in message
    news:xHvLb.16494$...
    > LaBird wrote:
    > > Dear all,
    > >
    > > I would like to ask what is the way to separate class
    > > template definition from declaration in different source
    > > files?

    >
    > No. (Your compiler doesn't support the "export" keyword.)
    >
    > The nearest hack-around is
    >
    > // foo.hpp
    > template<typename T> class X{
    > void func(const T&);
    > }
    > #include "foo.cpp"
    >
    > // foo.cpp
    > template<typename T> void X<T>::func(const T&){}
    >
     
    LaBird, Jan 9, 2004
    #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. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  2. blueblueblue2005

    undefined template reference again ??

    blueblueblue2005, Jul 24, 2005, in forum: C++
    Replies:
    9
    Views:
    374
    Zorro
    Jul 24, 2005
  3. Replies:
    1
    Views:
    2,129
    Gianni Mariani
    Jun 8, 2007
  4. nguillot
    Replies:
    5
    Views:
    539
  5. A L
    Replies:
    1
    Views:
    516
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page