Templates and inheritance in g++.

Discussion in 'C++' started by Maciej Zobniow, Feb 20, 2010.

  1. Hi everybody!

    I have problem with usage of inheritance and templates in my program
    compiled under g++.
    I have a base class A:

    template<class T>
    class A {
    public:
    A();
    A(const A& orig);
    virtual ~A();

    private:

    };

    and child class B:

    clacc B : public A<uint8_t>{
    public:
    B();
    B(const B& orig);
    ~B();
    }

    The class A is initializing a structure, then B adds some buffers to the
    structure. In implementation of construction of B I can write:

    B::B() : A<uint8_t>(){
    ....
    }
    ant this works.
    The problem happens in destructor. As I read in all books and
    turtorials, destructor of A is called just after destructor of B
    finished. Then I've implemented the destructor of B in the way that it
    frees only buffers allocated in B(). The destructor of A do the rest.
    As I can't write similar to constructor:
    B::~B():~A<uint8_t>(){...}

    in my code I have:

    B::~B(){
    ....
    }

    Program compiles, but linking fails with a message:

    undefined reference to A::~A<unsigned char>()

    I guess that if compiler do not see any implicit reference to
    A::~A<uint8_>() it's do not compile the specialization of the template.
    How to force the compiler to compile the specialization in the best way?

    Thanks
    Maciek
    Maciej Zobniow, Feb 20, 2010
    #1
    1. Advertising

  2. Maciej Zobniow

    Robert Fendt Guest

    And thus spake Maciej Zobniow <>
    Sat, 20 Feb 2010 15:30:27 +0100:

    > Program compiles, but linking fails with a message:
    >
    > undefined reference to A::~A<unsigned char>()
    >
    > I guess that if compiler do not see any implicit reference to
    > A::~A<uint8_>() it's do not compile the specialization of the template.
    > How to force the compiler to compile the specialization in the best way?


    Just a hunch, but s the destructor defined outside the class
    template? Then it's not surprising no code is generated for it.
    You can either pull it inside the template, or explicitely mark
    it inline (which should cause the destructor template
    to be instantiated along with the rest of the class).

    Regards,
    Robert
    Robert Fendt, Feb 20, 2010
    #2
    1. Advertising

  3. Right!

    Thx & Rgrds
    Maciek


    Robert Fendt wrote:
    > And thus spake Maciej Zobniow <>
    > Sat, 20 Feb 2010 15:30:27 +0100:
    >
    >> Program compiles, but linking fails with a message:
    >>
    >> undefined reference to A::~A<unsigned char>()
    >>
    >> I guess that if compiler do not see any implicit reference to
    >> A::~A<uint8_>() it's do not compile the specialization of the template.
    >> How to force the compiler to compile the specialization in the best way?

    >
    > Just a hunch, but s the destructor defined outside the class
    > template? Then it's not surprising no code is generated for it.
    > You can either pull it inside the template, or explicitely mark
    > it inline (which should cause the destructor template
    > to be instantiated along with the rest of the class).
    >
    > Regards,
    > Robert
    >
    Maciej Zobniow, Feb 20, 2010
    #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. JKop
    Replies:
    3
    Views:
    450
  2. Generic Usenet Account
    Replies:
    3
    Views:
    797
    Generic Usenet Account
    Jul 14, 2005
  3. BigMan
    Replies:
    1
    Views:
    423
  4. recover
    Replies:
    2
    Views:
    783
    recover
    Jul 25, 2006
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    586
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page