Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates problem

Reply
Thread Tools

templates problem

 
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      01-18-2006
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/
 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      01-18-2006
On Wed, 18 Jan 2006 11:46:04 +0200, Giannis Papadopoulos
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      01-18-2006
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
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-18-2006
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-lit...html#faq-35.13.

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

Best regards,

Tom

 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-18-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments