473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

templates problem

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/
Jan 18 '06 #1
4 1594
On Wed, 18 Jan 2006 11:46:04 +0200, Giannis Papadopoulos
<ip******@inf.uth.gr> 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

Jan 18 '06 #2
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
Jan 18 '06 #3
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

Jan 18 '06 #4
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

Jan 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to develop some nice code on solaris using the Forte C++...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
5
by: Eric Fortier | last post by:
Hi all, Last year I posted a message regarding templates use in blitter functions, but the single answer I got didn't help. My problem is that I was writing blitter functions which takes a...
1
by: jimmyB | last post by:
Hi, I have one query and I hope you people can solve it. Problem is related to Smarty Caching Database Templates.. Most popular web applications and forums like IPB (invision power board) and...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.