473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

template, specialization and header

Hello,

I'm writing a generic class in c++ with templates ; I
wish to specialize one of its function. All the code is
in the header file, with the #ifndef/#endif macro to
avoid multiple inclusion of the header. But when i
include my header in other files (so more than one
include), the linker complaints about the specialized
function already defined in each file (error LNK2005).
Why ???

Here is the example code :

in test.h :

#ifndef _TEST_
#define -TEST_

#include <iostream>

template <class T>
class test
{
public:
test() {};
void print();
};

template <class T>
void test<T>::print()
{ cout << "hello" << endl; }

template <>
void test<int>::print()
{ cout << "hello int" << endl; }

in main.cpp :

#include "test.h"
#include "test2.h"

void main()
{
test<char> a;
a.print();
test<int> b;
b.print();
}

in test2.h

#include "test.h"
Nov 16 '05 #1
1 1627
Thibault wrote:
Hello,

I'm writing a generic class in c++ with templates ; I
wish to specialize one of its function. All the code is
in the header file, with the #ifndef/#endif macro to
avoid multiple inclusion of the header. But when i
include my header in other files (so more than one
include), the linker complaints about the specialized
function already defined in each file (error LNK2005).
Why ???


Because an explicit specialization of a template function is not itself a
template. It's an ordinary function whose name is a template-id, in
standardese. If you want to have an explicit specialization be inline, you
need to say so:

template <class T>
void test<T>::print()
{ cout << "hello" << endl; }

template <>
inline void test<int>::print()
{ cout << "hello int" << endl; }

HTH

-cd
Nov 16 '05 #2

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

Similar topics

1
1552
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
4
1834
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
5
2235
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
1
2125
by: Bari | last post by:
Hi, When i do the following specializition which commented in the source then including the header in two cpp files gives me an error: test3.obj : error LNK2005: "class A<int* __cdecl...
9
3444
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
13
6550
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
1982
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
1
2202
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
5
3757
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
6967
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...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.