T
Thibault
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>:rint()
{ cout << "hello" << endl; }
template <>
void test<int>:rint()
{ 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"
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>:rint()
{ cout << "hello" << endl; }
template <>
void test<int>:rint()
{ 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"