I'm having troubles linking together different classes that uses the same templated class.
I found 2 workarounds, that will make it compile, but
it's not really a solution.
1. don't do a staged compiling with linking object code
2. inlining the spezialized member functions.
I'm looking for a solution that will make my templated class work just as nicely as the official stl's.
TemplatedFoo.h is an include guarded class definition that includes the implemention directly from TemplatedFoo.impl.
class1.cpp includes TemplatedFoo.h, and i can compile object code with 'g++ -c class1.cpp',
But when i try to link it with other stuff I get these kind of errors.
g++ ultra.cpp class1.o
class1.o: In function `Foo<float>::Foo(float)':
class1.cpp:(.text+0x128): multiple definition of `Foo<float>::Foo(float)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x6c): first defined here
class1.o: In function `Foo<float>::Foo(float)':
class1.cpp:(.text+0x17e): multiple definition of `Foo<float>::Foo(float)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x98): first defined here
class1.o: In function `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
class1.cpp:(.text+0x1aa): multiple definition of `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x1ae): first defined here
class1.o: In function `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
class1.cpp:(.text+0x1fa): multiple definition of `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x1fe): first defined here
collect2: ld returned 1 exit status
This is the code.
//TemplatedFoo.h
#ifndef TEMPLATED_FOO_H
#define TEMPLATED_FOO_H
template <typename T>
class Foo
{
public:
Foo(const T value);
T getValue() const;
private:
T m_value;
};
#include "TemplatedFoo.impl"
//TemplatedFoo.impl
template <>
Foo<float>::Foo(const float value ) {
std::cout << "I do floats:\n";
m_value = (value);
}
template <>
Foo<std::string>::Foo(const std::string val){
std::cout << "I don't do strings:\n";
}
template <typename T>
T Foo<T>::getValue() const
{
return m_value;
}
//class1.h
#include "TemplatedFoo.h"
class class1{
public:
class1(void);
~class1(void);
int dummy_method(void);
Foo<float> dummy_method2(void);
};
//class1.cpp
#include <iostream>
#include "TemplatedFoo.h"
#include "class1.h"
class1::class1(void){std::cout << "object constructed" << std::endl;}
class1::~class1(void){std::cout <<"object destructed" << std::endl;}
int class1::dummy_method(void){
int ret = 2;
return ret;
}
Foo<float> class1::dummy_method2(void){
Foo<float> ret(4.0);
return ret;
}
#include <iostream>
#include "class1.h"
int main(){
std::cout<<"start of main\n";
class1 cls1;
std::cout <<"calling dummy method: "<< cls1.dummy_method()<<std::endl;
Foo<float> var = cls1.dummy_method2();
std::cout <<"calling dummy method2: " <<var.getValue()<<std::endl;
std::cout<<"end of main\n";
}
//ultramain.cpp
#include <iostream>
#include "TemplatedFoo.h"
#include "class1.h"
int main(){
std::cout<<"start of main\n";
class1 cls1;
std::cout <<"calling dummy method: "<< cls1.dummy_method()<<std::endl;
Foo<float> var = cls1.dummy_method2();
std::cout <<"calling dummy method2: " <<var.getValue()<<std::endl;
std::cout<<"end of main\n";
}
This is just a simplified class of my larger program
thanks in advance