473,320 Members | 2,024 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,320 software developers and data experts.

template class member function specialization

Hi!

I am trying to specialize a member function of a template class (see
code below).

The moment the header with the "template class definition" and
"template class member function specialization" is included in two
different translation units (cpp-files).

Problem:
g++ 3.3.3 (or more exact the ld linker 2.14.90.0.7) complains about
multiple definitions / multiple symbols with the same name...

Intels c++ compiler (icc 8.0) compiles the same code without any
problem and links it.
Am I doing something wrong (C++ in a Nutshell shows that code like mine
should work on page 194) or is this a compiler/linker problem?
Kind regards,
Bjoern
Code:
=====

template_class.hpp - contains the specialized template class member
function "type"
------------------------------------------------------------------------------------------------------------------
#ifndef

#ifndef INCLUDED_template_class_HPP
#define INCLUDED_template_class_HPP

template< typename T >
class template_class {
public:
int num() const;
};

template< typename T >
int
template_class<T>::num() const
{
return 0;
}
template<>
int
template_class<float>::num() const
{
return 42;
}
#endif // INCLUDED_template_class_HPP
main.cpp - uses the template class
-----------------------------------------------
#include <iostream>

#include "template_class.hpp"
#include "linker_tester.hpp"

int main (int argc, char * const argv[]) {

(void) argc; // Prevent parameter not used warning.
(void) argv; // Prevent parameter not used warning.

template_class< int > a;
template_class< float > b;

std::cout << "a.num() " << a.num() << std::endl;
std::cout << "b.num() " << b.num() << std::endl;

output();

return 0;
}
linker_tester.hpp - declares a simple function that uses the template class
---------------------------------------------------------------------------------------------------
#ifndef

#ifndef INCLUDED_linker_tester_HPP
#define INCLUDED_linker_tester_HPP

int output();
#endif // INCLUDED_linker_tester_HPP
linker_tester.cpp
----------------------
#include "linker_tester.hpp"

#include <iostream>

#include "template_class.hpp"

int output()
{
template_class< int > a;
template_class< float > b;

std::cout << "output a.num() " << a.num() << std::endl;
std::cout << "output b.num() " << b.num() << std::endl;

return 0;
}

Jul 23 '05 #1
2 1650
Bjoern Knafla wrote:
I am trying to specialize a member function of a template class (see
code below).

The moment the header with the "template class definition" and "template
class member function specialization" is included in two different
translation units (cpp-files).

Problem:
g++ 3.3.3 (or more exact the ld linker 2.14.90.0.7) complains about
multiple definitions / multiple symbols with the same name...

Intels c++ compiler (icc 8.0) compiles the same code without any
problem and links it.
Am I doing something wrong (C++ in a Nutshell shows that code like mine
should work on page 194) or is this a compiler/linker problem?
[...]


It is a compiler/linker problem. I heard bad things about 3.3.3, try
upgrading to 3.4.

V
Jul 23 '05 #2
>
It is a compiler/linker problem. I heard bad things about 3.3.3, try
upgrading to 3.4.

V


Thank you!

Cheers,
Bjoern

Jul 23 '05 #3

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

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.