templates problem

Discussion in 'C++' started by Giannis Papadopoulos, Jan 18, 2006.

  1. I'm new to C++. I want to implement a simple example with a template class.

    I have written the following:

    A.hpp
    -----
    #ifndef A_H
    #define A_H

    template<class T> class A {
    private:
    T val;
    public:
    A(T v);
    ~A(void);
    T getA(void) const;
    };

    #endif


    A.cpp
    -----
    #include "A.hpp"

    template<class T> A<T>::A(T v) : val(v) {}

    template<class T> A<T>::~A(void) {}

    template<class T> T A<T>::getA(void) const {
    return this->val;
    }


    test2.cpp
    --------
    #include <iostream>
    #include "A.hpp"

    using namespace std;

    int main(void) {
    A<int> sth(5);

    return 0;
    }

    This compiles, but during linking it reports:

    test2.o(.text+0x26): In function `main':
    test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
    test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
    [in-charge]()'
    collect2: ld returned 1 exit status

    However, if I implement everything in A.hpp, it works fine. Am I doing
    something wrong or is this a compiler issue?

    I am using gcc 3.3.5.

    --
    one's freedom stops where others' begin

    Giannis Papadopoulos
    Computer and Communications Engineering dept. (CCED)
    University of Thessaly
    http://dop.freegr.net/
    Giannis Papadopoulos, Jan 18, 2006
    #1
    1. Advertising

  2. Giannis Papadopoulos

    Zara Guest

    On Wed, 18 Jan 2006 11:46:04 +0200, Giannis Papadopoulos
    <> wrote:

    >I'm new to C++. I want to implement a simple example with a template class.
    >
    >I have written the following:
    >

    <snip>
    >
    >This compiles, but during linking it reports:
    >
    >test2.o(.text+0x26): In function `main':
    >test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
    >test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
    >[in-charge]()'
    >collect2: ld returned 1 exit status
    >
    >However, if I implement everything in A.hpp, it works fine. Am I doing
    >something wrong or is this a compiler issue?
    >
    >I am using gcc 3.3.5.



    The template definition should be available to the compiler/linker.
    This may be done by:

    a) Using the export keyword in A.cpp. This will not work on most
    compilers, although it is standard C++. I think GCC does not support
    it.

    b) Including the defintion in A.hpp, as you have noted.

    c) Explicitly instantiating the template for the types you need,
    within A.cpp. This is the worst way to do it, as you must keep track
    of the instantiations needed.

    (b) is the most used option, for instance in all standara library
    implementations, and in boost libraries,and is compiler independent
    and, as such, portable

    Zara
    Zara, Jan 18, 2006
    #2
    1. Advertising

  3. Giannis Papadopoulos wrote:
    > I'm new to C++. I want to implement a simple example with a template class.
    >
    > I have written the following:
    >
    > A.hpp
    > -----
    > #ifndef A_H
    > #define A_H
    >
    > template<class T> class A {
    > private:
    > T val;
    > public:
    > A(T v);
    > ~A(void);
    > T getA(void) const;
    > };
    >
    > #endif
    >
    >
    > A.cpp
    > -----
    > #include "A.hpp"
    >
    > template<class T> A<T>::A(T v) : val(v) {}
    >
    > template<class T> A<T>::~A(void) {}
    >
    > template<class T> T A<T>::getA(void) const {
    > return this->val;
    > }
    >
    >
    > test2.cpp
    > --------
    > #include <iostream>
    > #include "A.hpp"
    >
    > using namespace std;
    >
    > int main(void) {
    > A<int> sth(5);
    >
    > return 0;
    > }
    >
    > This compiles, but during linking it reports:
    >
    > test2.o(.text+0x26): In function `main':
    > test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
    > test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
    > [in-charge]()'
    > collect2: ld returned 1 exit status
    >
    > However, if I implement everything in A.hpp, it works fine. Am I doing
    > something wrong or is this a compiler issue?
    >
    > I am using gcc 3.3.5.
    >


    no - this is the expected behaviour with most compilers. Template code
    must be completely implemented in a .h file. There are some compilers
    that support template code in .cpp files, but gcc is currently not one
    of them...

    Tom
    Thomas Maier-Komor, Jan 18, 2006
    #3
  4. Giannis Papadopoulos wrote:

    > I have written the following:
    >
    > A.hpp
    > -----
    > #ifndef A_H
    > #define A_H
    >
    > template<class T> class A {
    > private:
    > T val;
    > public:
    > A(T v);
    > ~A(void);
    > T getA(void) const;
    > };
    >
    > #endif
    >
    >
    > A.cpp
    > -----
    > #include "A.hpp"
    >
    > template<class T> A<T>::A(T v) : val(v) {}
    >
    > template<class T> A<T>::~A(void) {}
    >
    > template<class T> T A<T>::getA(void) const {
    > return this->val;
    > }
    >
    >
    > test2.cpp
    > --------
    > #include <iostream>
    > #include "A.hpp"
    >
    > using namespace std;
    >
    > int main(void) {
    > A<int> sth(5);
    >
    > return 0;
    > }
    >
    > This compiles, but during linking it reports:
    >
    > test2.o(.text+0x26): In function `main':
    > test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
    > test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
    > [in-charge]()'
    > collect2: ld returned 1 exit status
    >
    > However, if I implement everything in A.hpp, it works fine. Am I doing
    > something wrong or is this a compiler issue?


    This is an FAQ:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.13.

    You should take a look at the preceding FAQ (35.12) as well.

    Best regards,

    Tom
    Thomas Tutone, Jan 18, 2006
    #4
  5. Giannis Papadopoulos

    Luke Meyers Guest

    Thomas Maier-Komor wrote:
    > Template code must be completely implemented in a .h file.


    Not entirely true. There are multiple ways to address this issue.
    Putting all template definitions in a .h file is just one of them.

    Luke
    Luke Meyers, Jan 18, 2006
    #5
    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. Fred
    Replies:
    1
    Views:
    578
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    372
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    450
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    355
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    783
    recover
    Jul 25, 2006
Loading...

Share This Page