templates problem

  • Thread starter Giannis Papadopoulos
  • Start date
G

Giannis Papadopoulos

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/
 
Z

Zara

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

I have written the following:
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
 
T

Thomas Maier-Komor

Giannis said:
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
 
T

Thomas Tutone

Giannis said:
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
 
L

Luke Meyers

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top