[Q] Template: Undefined Constructors

Discussion in 'C++' started by Eric, Apr 19, 2004.

  1. Eric

    Eric Guest

    Ok, this is very strange.

    in TRegionGrower.h, I've got:

    --
    template <class T>
    class TRegionGrower
    {
    public:

    TRegionGrower( void );
    ~TRegionGrower( void );
    };
    --

    In TRegionGrower.cp, I've got:

    --
    #include "TRegionGrower.h"

    template <class T>
    TRegionGrower<T>::
    TRegionGrower( void )
    {

    }



    template <class T>
    TRegionGrower<T>::
    ~TRegionGrower( void )
    {

    }
    --

    I use this class by doing:

    TRegionGrower<UInt8> regionGrower;

    I get two link errors telling me that the constructor and destructor are
    undefined.

    However, if I remove the constructor and destructors from the .cp file
    and change the .h file to:

    --
    template <class T>
    class TRegionGrower
    {
    public:

    TRegionGrower( void )
    {
    }

    ~TRegionGrower( void )
    {
    }
    };
    --

    The link errors go away.

    Any idea what might be going on here?
    Eric, Apr 19, 2004
    #1
    1. Advertising

  2. Eric wrote:
    >
    > I get two link errors telling me that the constructor and destructor are
    > undefined.
    >
    > However, if I remove the constructor and destructors from the .cp file
    > and change the .h file to:
    >
    > template <class T>
    > class TRegionGrower
    > {
    > public:
    >
    > TRegionGrower( void )
    > {
    > }
    >
    > ~TRegionGrower( void )
    > {
    > }
    > };
    >
    > The link errors go away.


    The FAQ addresses this issue:

    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.12

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, Apr 20, 2004
    #2
    1. Advertising

  3. On Mon, 19 Apr 2004, Eric wrote:

    >Ok, this is very strange.
    >
    >in TRegionGrower.h, I've got:
    >
    >--
    >template <class T>
    >class TRegionGrower
    >{
    >public:
    >
    > TRegionGrower( void );
    > ~TRegionGrower( void );
    >};
    >--
    >
    >In TRegionGrower.cp, I've got:
    >
    >--
    >#include "TRegionGrower.h"
    >
    >template <class T>
    >TRegionGrower<T>::
    >TRegionGrower( void )
    >{
    >
    >}
    >
    >
    >
    >template <class T>
    >TRegionGrower<T>::
    >~TRegionGrower( void )
    >{
    >
    >}
    >--
    >
    >I use this class by doing:
    >
    > TRegionGrower<UInt8> regionGrower;
    >
    >I get two link errors telling me that the constructor and destructor are
    >undefined.
    >


    1. How about giving us the definition of UInt8? With a sensible typedef,
    there shouldn't be any errors. If its a macro, anything is possible.

    2. Is the last line of code in a separate source file or in
    TRegionGrower.cp?

    3. What does the command line to (compile and) link the files look like?


    >However, if I remove the constructor and destructors from the .cp file
    >and change the .h file to:
    >
    >--
    >template <class T>
    >class TRegionGrower
    >{
    >public:
    >
    > TRegionGrower( void )
    > {
    > }
    >
    > ~TRegionGrower( void )
    > {
    > }
    >};
    >--
    >
    >The link errors go away.
    >
    >Any idea what might be going on here?
    >
    >
    >


    --
    Claudio Jolowicz
    Claudio Jolowicz, Apr 20, 2004
    #3
  4. (Eric) wrote in message news:<1gci5k9.17r0locvblnj0N%>...
    > Ok, this is very strange.
    >
    > in TRegionGrower.h, I've got:
    >
    > --
    > template <class T>
    > class TRegionGrower
    > {
    > public:
    >
    > TRegionGrower( void );
    > ~TRegionGrower( void );
    > };
    > --
    >
    > In TRegionGrower.cp, I've got:

    [ the obvious implementation ]

    The FAQ reference in the previous post is correct, although there
    currently is an alternative solution. These templates need extra
    bookkeeping, as the FAQ explains. This is enabled using the export
    keyword on the class, but it is today only supported on the Comeau
    compiler.

    Regards,
    Michiel Salters
    Michiel Salters, Apr 20, 2004
    #4
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,248
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    573
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    583
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    382
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    456
Loading...

Share This Page