Simple Template Question

Discussion in 'C++' started by LaBird, Jul 30, 2003.

  1. LaBird

    LaBird Guest

    Hi,

    I have a simple question about templates.

    When I have a class definition like this in "class.h":

    template <typename T>
    class C {
    T p;
    public:
    void set(T);
    void print(T);
    };

    And then I implement the member functions in "class.cpp":

    #include "class.h"

    template <typename T>
    void C<T>::set(T t) {
    // some meaningful stuff, setting p = t
    }

    template <typename T>
    void C<T>::print(T t) {
    // other meaningful stuff, printing out p
    }

    Now I would like to use the class in main() in "main.cpp".
    How should I call it? I tried the following:

    #include "class.h"

    int main() {
    C<int> object;
    object.set(7);
    // ...
    return 0;
    }

    But when I compile with "g++ class.cpp main.cpp",
    I get "undefined reference" for the C<T>::set(T) function
    in main.cpp. How to overcome this, apart from merging
    class.cpp and main.cpp together?

    Thanks in advance.

    Regards,
    LaBird (Benny).
    LaBird, Jul 30, 2003
    #1
    1. Advertising

  2. "LaBird" <> wrote in message
    news:bg8938$hib$...
    > Hi,
    >
    > I have a simple question about templates.
    >
    > When I have a class definition like this in "class.h":
    >
    > template <typename T>
    > class C {
    > T p;
    > public:
    > void set(T);
    > void print(T);
    > };
    >
    > And then I implement the member functions in "class.cpp":
    >
    > #include "class.h"
    >
    > template <typename T>
    > void C<T>::set(T t) {
    > // some meaningful stuff, setting p = t
    > }
    >
    > template <typename T>
    > void C<T>::print(T t) {
    > // other meaningful stuff, printing out p
    > }


    Copy all the functions into class.h.
    Throw away class.cpp.
    Josephine Schafer, Jul 30, 2003
    #2
    1. Advertising

  3. "LaBird" <> :
    > When I have a class definition like this in "class.h":
    >
    > template <typename T>
    > class C {
    > T p;
    > public:
    > void set(T);
    > void print(T);
    > };
    >
    > And then I implement the member functions in "class.cpp":

    [snip]
    > Now I would like to use the class in main() in "main.cpp".
    > How should I call it? I tried the following:
    >
    > #include "class.h"
    >
    > int main() {
    > C<int> object;
    > object.set(7);
    > // ...
    > return 0;
    > }
    >
    > But when I compile with "g++ class.cpp main.cpp",
    > I get "undefined reference" for the C<T>::set(T) function
    > in main.cpp. How to overcome this, apart from merging
    > class.cpp and main.cpp together?


    If you want to put the implementation of template function into a .cpp
    file, you will need a compiler that supports the 'export' keyword, at
    this moment very few compilers do. For one that supports 'export' see:
    http://www.comeaucomputing.com. Without the 'export' keyword you will
    have to put inline definitions of the functions into the class.h header
    file.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Jul 30, 2003
    #3
  4. LaBird

    Agent Mulder Guest

    LB> I would like to use the class in main() in "main.cpp".
    LB> How should I call it?

    Stick to this style and you'll never
    have any problems with your #include's.



    //This is file C.h. All headers needed go to main.cpp
    template<typename T>class C
    {
    T p;
    public:
    void set(T t){p=t;}
    void print(T t){cout<<"\nif it ain't got that swing";}
    };

    //This if file Music.h. All headers needed go to main.cpp
    class Music{};

    //This is file Jazz.h. All headers needed go to main.cpp
    class Jazz:public C<Music>{};

    //This is file main.cpp. All #include's go here
    #include <iostream.h>
    #include "C.h"
    #include "Music.h"
    #include "Jazz.h"
    int main(int argv,char**argc)
    {
    C<int>object;
    object.set(7);
    Jazz jazz;
    jazz.print(*new Music);
    return 0;
    }

    -----------------------------------
    output

    if it ain't got that swing
    Agent Mulder, Jul 30, 2003
    #4
  5. LaBird

    LaBird Guest

    Josephine Schafer <> wrote in message
    news:bg8a1b$ldnki$-berlin.de...
    >
    > Copy all the functions into class.h.
    > Throw away class.cpp.
    >


    A follow-up question: As my actual class definiton is more complicated,
    and the member functions of the class contain lots of accesses to global
    variables, which are initialized at the global scope, something like:

    unsigned int count = 0;
    template<typename T> class C<T>::set(T t) {
    if (count % 2 == 0)
    p = t;
    else
    p = 0 - t;
    count++;
    }

    Can I put all these code into the .h as well?

    Regards,
    LaBird (Benny).
    LaBird, Jul 30, 2003
    #5
  6. LaBird

    LaBird Guest

    Thank you very much, I did not realize before that my question is actually
    not as simple as I thought to be. :)

    Regards,
    LaBird (Benny).


    Peter van Merkerk <> wrote in message
    news:bg8a7g$l05ug$-berlin.de...
    > If you want to put the implementation of template function into a .cpp
    > file, you will need a compiler that supports the 'export' keyword, at
    > this moment very few compilers do. For one that supports 'export' see:
    > http://www.comeaucomputing.com. Without the 'export' keyword you will
    > have to put inline definitions of the functions into the class.h header
    > file.
    LaBird, Jul 30, 2003
    #6
  7. Josephine Schafer wrote:

    >
    > "LaBird" <> wrote in message
    > news:bg8938$hib$...
    >> Hi,
    >>
    >> I have a simple question about templates.
    >>
    >> When I have a class definition like this in "class.h":
    >>
    >> template <typename T>
    >> class C {
    >> T p;
    >> public:
    >> void set(T);
    >> void print(T);
    >> };
    >>
    >> And then I implement the member functions in "class.cpp":
    >>
    >> #include "class.h"
    >>
    >> template <typename T>
    >> void C<T>::set(T t) {
    >> // some meaningful stuff, setting p = t
    >> }
    >>
    >> template <typename T>
    >> void C<T>::print(T t) {
    >> // other meaningful stuff, printing out p
    >> }

    >
    > Copy all the functions into class.h.
    > Throw away class.cpp.


    Is the STL implemented that way? Everything in the headers and no .cpp's?

    Is it possible at all to write a "template library" and link dynamically
    against it? I guess it would be possible since all necessary function
    pointers should be available, but they would have to be determined at
    runtime for every template call...?
    Harald Grossauer, Jul 30, 2003
    #7
  8. "LaBird" <> wrote in message
    news:bg8o9c$ngb$...
    > Josephine Schafer <> wrote in message
    > news:bg8a1b$ldnki$-berlin.de...
    > >
    > > Copy all the functions into class.h.
    > > Throw away class.cpp.
    > >

    >
    > A follow-up question: As my actual class definiton is more complicated,
    > and the member functions of the class contain lots of accesses to global
    > variables, which are initialized at the global scope, something like:
    >
    > unsigned int count = 0;
    > template<typename T> class C<T>::set(T t) {
    > if (count % 2 == 0)
    > p = t;
    > else
    > p = 0 - t;
    > count++;
    > }
    >
    > Can I put all these code into the .h as well?


    See anything preceded by a template<...> means compiler won't allocate
    storage for it at that point, but will instead wait until the template
    instantiation.
    Also somwhere in the linker and compiler there's a mechanism to remove
    multiple definitions of identical templates. So for ease of use you would
    almost always put the entire template declarartion and definition in the
    header file.
    Josephine Schafer, Jul 31, 2003
    #8
  9. LaBird

    Agent Mulder Guest

    LB> A follow-up question: As my actual class definiton is more complicated,
    LB> and the member functions of the class contain lots of accesses to global
    LB> variables, which are initialized at the global scope, something like:
    LB>
    LB> unsigned int count = 0;
    LB> template<typename T> class C<T>::set(T t) {
    LB> if (count % 2 == 0)
    LB> p = t;
    LB> else
    LB> p = 0 - t;
    LB> count++;
    LB> }
    LB>
    LB> Can I put all these code into the .h as well?

    Yes, that's my style. When I work on global
    instances of my objects, I create the objects at the start
    of the .h file and declare the class or template that
    works on the global instances right under it.
    The objects are encapsulated, not at class level
    but at module (file) level. Works fine.

    -X
    Agent Mulder, Jul 31, 2003
    #9
    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. Chris Theis
    Replies:
    2
    Views:
    453
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    520
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,081
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    754
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    514
Loading...

Share This Page