undefined template reference again ??

Discussion in 'C++' started by blueblueblue2005, Jul 24, 2005.

  1. Here is a very simple template example, whenever I seperate the header
    and implementation file, I got "undefined reference to member function
    error. and if I put the header and definition in one file which is
    still named as .h file, it compiled and run.

    here are files:

    // Listnode.h
    #ifndef LISTNODE_H
    #define LISTNODE_H

    template< typename NODETYPE >
    class ListNode
    {
    public:
    ListNode( const NODETYPE & );
    NODETYPE getData() const;
    private:
    NODETYPE data;
    ListNode< NODETYPE > *nextPtr;
    };

    #endif



    // Listnode.cpp
    #include "Listnode.h"

    // constructor
    template< typename NODETYPE >
    ListNode< NODETYPE >::ListNode( const NODETYPE &info )
    : data( info ), nextPtr( 0 )
    {
    }
    // return copy of data in node
    template< typename NODETYPE >
    NODETYPE ListNode< NODETYPE >::getData() const
    {
    return data;
    }




    // t.cpp -- testing file
    #include <iostream>
    #include "Listnode.h"

    using namespace std;

    int main() {
    ListNode<int> n(89);
    cout << n.getData() << endl;
    return 0;
    }
    blueblueblue2005, Jul 24, 2005
    #1
    1. Advertising

  2. blueblueblue2005

    Zenith Guest

    How do u compile these files? I guess you might key in something like,
    g++ t.cpp
    That is sure to fail if you seperate Listnode.h and its implementation
    cpp file, as from t.cpp only you cannot reach the implementation.
    You may first compile a listnode.o from listnode.h and listnode.cpp,
    and then compile t.cpp with the object file. A simple makefile should
    work fine.
    Zenith, Jul 24, 2005
    #2
    1. Advertising

  3. I compiled all the .cpp file

    g++ t.cpp Listnode.cpp
    blueblueblue2005, Jul 24, 2005
    #3
  4. It seems for class template, the header and implementation must be in
    one file. I just checkedt the Deital book, all its template examples
    are in this way.
    blueblueblue2005, Jul 24, 2005
    #4
  5. blueblueblue2005 wrote:
    > It seems for class template, the header and implementation must be in
    > one file. I just checkedt the Deital book, all its template examples
    > are in this way.
    >


    Lookup keyword export

    http://www.kuzbass.ru:8086/docs/isocpp/template.html

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
    Tobias Blomkvist, Jul 24, 2005
    #5
  6. blueblueblue2005 wrote:
    > It seems for class template, the header and implementation must be in
    > one file. I just checkedt the Deital book, all its template examples
    > are in this way.
    >


    Or, at the end of the header-file you explicitly include the cpp-file,
    and wrap the definitions within a macro-define scoped around the include.

    #<template.h>--------------------------

    template<typename T>
    class test {
    public:
    void f();
    };

    #define INCLUDE_SHEILD
    #include "template.cpp"
    #undef INCLUDE_SHEILD

    #--------------------------------------

    #<template.cpp>------------------------

    #ifdef INCLUDE_SHEILD

    template<typename T>
    void test<T>::f() {}

    #endif

    #--------------------------------------

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
    Tobias Blomkvist, Jul 24, 2005
    #6
  7. well, neither way works :(
    blueblueblue2005, Jul 24, 2005
    #7
  8. blueblueblue2005 sade:
    > well, neither way works :(
    >


    Well, the export keyword is badly supported by compilers as
    I understand it, but the other given solution works perfectly
    for me and my borland environment, I often use it to split
    the template declarations and definitions into different units.

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
    Tobias Blomkvist, Jul 24, 2005
    #8
  9. blueblueblue2005

    Ben Pope Guest

    Tobias Blomkvist wrote:
    > blueblueblue2005 wrote:
    >
    >> It seems for class template, the header and implementation must be in
    >> one file. I just checkedt the Deital book, all its template examples
    >> are in this way.
    >>

    >
    > Or, at the end of the header-file you explicitly include the cpp-file,
    > and wrap the definitions within a macro-define scoped around the include.


    ....or explicitly instantiate the template(s) you require, at the bottom of the cpp-file.

    Ben
    --
    I'm not just a number. To many, I'm known as a String...
    Ben Pope, Jul 24, 2005
    #9
  10. blueblueblue2005

    Zorro Guest

    I am replying to your attempt of separating template defs from the
    bodies of methods. The best way to think about this (within the context
    of C++) is as follows.

    At the point of instantiation, for instance,

    my_class<some_type> object;

    the compiler needs access to the bodies so it can generate code. That
    is why the bodies are included in header file.

    There are other complications that can result in linkage errors. But
    this will help you with the problem you are posting.

    Hope it helps.
    Z.
    Zorro, Jul 24, 2005
    #10
    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. amit
    Replies:
    0
    Views:
    388
  2. che
    Replies:
    2
    Views:
    490
  3. LaBird
    Replies:
    2
    Views:
    6,900
    LaBird
    Jan 9, 2004
  4. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    326
    Maric Michaud
    Jun 9, 2006
  5. Replies:
    4
    Views:
    403
Loading...

Share This Page