Connecting Tech Pros Worldwide Help | Site Map

templates problem

Giannis Papadopoulos
Guest
 
Posts: n/a
#1: Jan 18 '06
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/
Zara
Guest
 
Posts: n/a
#2: Jan 18 '06

re: templates problem


On Wed, 18 Jan 2006 11:46:04 +0200, Giannis Papadopoulos
<ipapadop@inf.uth.gr> wrote:
[color=blue]
>I'm new to C++. I want to implement a simple example with a template class.
>
>I have written the following:
>[/color]
<snip>[color=blue]
>
>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.[/color]


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

Thomas Maier-Komor
Guest
 
Posts: n/a
#3: Jan 18 '06

re: templates problem


Giannis Papadopoulos wrote:[color=blue]
> 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.
>[/color]

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 Tutone
Guest
 
Posts: n/a
#4: Jan 18 '06

re: templates problem


Giannis Papadopoulos wrote:
[color=blue]
> 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?[/color]

This is an FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.13.

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

Best regards,

Tom

Luke Meyers
Guest
 
Posts: n/a
#5: Jan 18 '06

re: templates problem


Thomas Maier-Komor wrote:[color=blue]
> Template code must be completely implemented in a .h file.[/color]

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

Closed Thread


Similar C / C++ bytes