template separation

K

klaas

Can I put implementation and header class definition in separate files
also for templatized classes?
I get a lot of errors and i don't know why.
I make file like:
gekko.h
#ifndef GEKKO_h
#define GEKKO_h
#include <iostream>

using std::cout;
using std::endl;
namespace gekko
{
template <class d>
class d
{public:
d();
}
/*end of gekko*/
}
#endif

gekko.cpp
#ifndef GEKKO_cpp
#define GEKKO_cpp
#include <iostream>

using std::cout;
using std::endl;
namespace gekko
{
template <class d>
gekko<d>::gekko(){cout << "something happens" << endl;}

/*end of gekko*/
}
#endif

this way I just creat a main program which includes
#include "gekko.h"

I get lot's of weird errors in the linking step, you'll see it is as if
the standard library function were not linked or something?????
thanks in advance,

hendrik

actual errors:

bprop.o(.bss+0x0):bprop.cpp: multiple definition of `psyche::debug'
psyche.o(.bss+0x0):psyche.cpp: first defined here
NN.o(.bss+0x0):NN.cpp: multiple definition of `psyche::debug'
psyche.o(.bss+0x0):psyche.cpp: first defined here
psyche.o(.text+0x5f):psyche.cpp: undefined reference to
`psyche::perceptron<floa
t>::perceptron[in-charge]()'
psyche.o(.text+0xc6):psyche.cpp: undefined reference to `std::cout'
psyche.o(.text+0xd3):psyche.cpp: undefined reference to
`std::basic_ostream<char
(std::basic
_ostream<char, std::char_traits<char> >&, char const*)'
psyche.o(.text+0xde):psyche.cpp: undefined reference to
`std::basic_ostream<char
(std::basic_
ostream<char, std::char_traits<char> >&)'
psyche.o(.text+0xe3):psyche.cpp: undefined reference to
`std::basic_ostream<char
, std::char_traits<char> >::eek:perator<<(std::basic_ostream<char,
std::char_traits
<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
psyche.o(.text+0x13a):psyche.cpp: undefined reference to
`psyche::backprop<float
, psyche::data, psyche::sigmoid>::eek:ffsets(std::vector<int,
std::allocator<int> >
&)'
psyche.o(.text+0x149):psyche.cpp: undefined reference to `std::cout'
psyche.o(.text+0x156):psyche.cpp: undefined reference to
`std::basic_ostream<cha
(std::basi
c_ostream<char, std::char_traits<char> >&, char const*)'
psyche.o(.text+0x16a):psyche.cpp: undefined reference to
`std::basic_ostream<cha
(std::basi
c_ostream<char, std::char_traits<char> >&, char const*)'
psyche.o(.text+0x175):psyche.cpp: undefined reference to
`std::basic_ostream<cha
(std::basic
_ostream<char, std::char_traits<char> >&)'
psyche.o(.text+0x17a):psyche.cpp: undefined reference to
`std::basic_ostream<cha
r, std::char_traits<char> >::eek:perator<<(std::basic_ostream<char,
std::char_trait
s<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
psyche.o(.text+0x188):psyche.cpp: undefined reference to
`std::allocator<char>::
allocator[in-charge]()'
psyche.o(.text+0x1a9):psyche.cpp: undefined reference to
`std::basic_string<char
::basic_string[in-charge](char c
onst*, std::allocator<char> const&)'
psyche.o(.text+0x1bb):psyche.cpp: undefined reference to
`psyche::perceptron<flo
at>::file(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >
)'
psyche.o(.text+0x1d4):psyche.cpp: undefined reference to
`std::basic_string<char
, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()'
psyche.o(.text+0x1e7):psyche.cpp: undefined reference to
`std::basic_string<char
, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()'
psyche.o(.text+0x203):psyche.cpp: undefined reference to
`std::allocator<char>::
~allocator [in-charge]()'
psyche.o(.text+0x219):psyche.cpp: undefined reference to
`std::allocator<char>::
~allocator [in-charge]()'
psyche.o(.text+0x2a7):psyche.cpp: undefined reference to
`std::ios_base::Init::I
nit[in-charge]()'
psyche.o(.text+0x2f8):psyche.cpp: undefined reference to
`std::ios_base::Init::~
Init [in-charge]()'
psyche.o(.text$_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iterato
rIPiS1_EERKi+0x1a5):psyche.cpp: undefined reference to `___cxa_begin_catch'
psyche.o(.text$_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iterato
rIPiS1_EERKi+0x1e1):psyche.cpp: undefined reference to `___cxa_rethrow'
psyche.o(.text$_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iterato
rIPiS1_EERKi+0x1ec):psyche.cpp: undefined reference to `___cxa_end_catch'
psyche.o(.text$_ZNSt14__simple_allocIiSt24__default_alloc_templateILb1ELi0EEE10d
eallocateEPij+0x1d):psyche.cpp: undefined reference to
`std::__default_alloc_tem
plate<(bool)1, (int)0>::deallocate(void*, unsigned)'
psyche.o(.text$_ZNSt14__simple_allocIiSt24__default_alloc_templateILb1ELi0EEE8al
locateEj+0x16):psyche.cpp: undefined reference to
`std::__default_alloc_template
<(bool)1, (int)0>::allocate(unsigned)'
psyche.o(.text$_ZNSt14__simple_allocISt6vectorIiSaIiEESt24__default_alloc_templa
teILb1ELi0EEE10deallocateEPS2_j+0x23):psyche.cpp: undefined reference to
`std::_
_default_alloc_template<(bool)1, (int)0>::deallocate(void*, unsigned)'
psyche.o(.text$_ZSt8_DestroyIN6psyche6matrixIdEEEvPT_+0xd):psyche.cpp:
undefined
reference to `psyche::matrix<double>::~matrix [in-charge]()'
psyche.o(.text$_ZNSt14__simple_allocIN6psyche6matrixIdEESt24__default_alloc_temp
lateILb1ELi0EEE10deallocateEPS2_j+0x24):psyche.cpp: undefined reference
to `std:
:__default_alloc_template<(bool)1, (int)0>::deallocate(void*, unsigned)'
psyche.o(.text$_ZSt8_DestroyIN6psyche6matrixIfEEEvPT_+0xd):psyche.cpp:
undefined
reference to `psyche::matrix<float>::~matrix [in-charge]()'
psyche.o(.text$_ZNSt14__simple_allocIN6psyche6matrixIfEESt24__default_alloc_temp
lateILb1ELi0EEE10deallocateEPS2_j+0x24):psyche.cpp: undefined reference
to `std:
:__default_alloc_template<(bool)1, (int)0>::deallocate(void*, unsigned)'
psyche.o(.eh_frame+0x12):psyche.cpp: undefined reference to
`___gxx_personality_
v0'
matrix.o(.text+0x1d):matrix.cpp: undefined reference to
`std::ios_base::Init::In
it[in-charge]()'
matrix.o(.text+0x38):matrix.cpp: undefined reference to
`std::ios_base::Init::~I
nit [in-charge]()'
matrix.o(.eh_frame+0x11):matrix.cpp: undefined reference to
`___gxx_personality_
v0'
test.o(.text+0x1d):test.cpp: undefined reference to
`std::ios_base::Init::Init[i
n-charge]()'
test.o(.text+0x38):test.cpp: undefined reference to
`std::ios_base::Init::~Init
[in-charge]()'
test.o(.eh_frame+0x11):test.cpp: undefined reference to
`___gxx_personality_v0'
bprop.o(.text+0x1d):bprop.cpp: undefined reference to
`std::ios_base::Init::Init
[in-charge]()'
bprop.o(.text+0x6e):bprop.cpp: undefined reference to
`std::ios_base::Init::~Ini
t [in-charge]()'
bprop.o(.text$_ZN6psyche8backpropIdNS_4dataENS_7sigmoidEEC1Ev+0xe):bprop.cpp:
un
defined reference to `psyche::topology<double>::topology[not-in-charge]()'
bprop.o(.text$_ZN6psyche8backpropIdNS_4dataENS_7sigmoidEEC1Ev+0x1c):bprop.cpp:
u
ndefined reference to `psyche::data<double>::data[not-in-charge]()'
bprop.o(.text$_ZN6psyche8backpropIdNS_4dataENS_7sigmoidEEC1Ev+0x27):bprop.cpp:
u
ndefined reference to `psyche::sigmoid<double>::sigmoid[not-in-charge]()'
bprop.o(.eh_frame+0x12):bprop.cpp: undefined reference to
`___gxx_personality_v0
'
topology.o(.text+0x1d):topology.cpp: undefined reference to
`std::ios_base::Init
::Init[in-charge]()'
topology.o(.text+0x38):topology.cpp: undefined reference to
`std::ios_base::Init
::~Init [in-charge]()'
topology.o(.eh_frame+0x11):topology.cpp: undefined reference to
`___gxx_personal
ity_v0'
dataobj.o(.text+0xe1):dataobj.cpp: undefined reference to
`std::basic_string<cha
r, std::char_traits<char>, std::allocator<char> >::length() const'
dataobj.o(.text+0xfd):dataobj.cpp: undefined reference to
`std::basic_string<cha
r, std::char_traits<char>, std::allocator<char> >::eek:perator[](unsigned)
const'
dataobj.o(.text+0x12b):dataobj.cpp: undefined reference to
`std::basic_string<ch
ar, std::char_traits<char>, std::allocator<char> >::substr(unsigned,
unsigned) c
onst'
dataobj.o(.text+0x153):dataobj.cpp: undefined reference to
`std::basic_string<ch
ar, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()'

dataobj.o(.text+0x163):dataobj.cpp: undefined reference to
`std::basic_string<ch
ar, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()'

dataobj.o(.text$_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_+0x14):dataobj
..cpp: undefined reference to `std::basic_string<char,
std::char_traits<char>, st
d::allocator<char> >::compare(std::basic_string<char,
std::char_traits<char>, st
d::allocator<char> > const&) const'
dataobj.o(.text$_ZSt10_ConstructISsSsEvPT_RKT0_+0x42):dataobj.cpp:
undefined ref
erence to `std::basic_string said:
::basic_string[in-charge](std::basic_string<char,
std::char_traits<char>, std::
allocator<char> > const&)'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0x49):dataobj.cpp: undefined reference to
`std::basic_string<c
::basic_string[in-charge](std
::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0xb8):dataobj.cpp: undefined reference to
`std::basic_string<c
::eek:perator=(std::basic_string
<char, std::char_traits<char>, std::allocator<char> > const&)'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0xcb):dataobj.cpp: undefined reference to
`std::basic_string<c
har, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()
'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0xe4):dataobj.cpp: undefined reference to
`std::basic_string<c
har, std::char_traits<char>, std::allocator<char> >::~basic_string
[in-charge]()
'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0x1e3):dataobj.cpp: undefined reference to
`___cxa_begin_catch
'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0x21f):dataobj.cpp: undefined reference to
`___cxa_rethrow'
dataobj.o(.text$_ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iter
atorIPSsS1_EERKSs+0x22a):dataobj.cpp: undefined reference to
`___cxa_end_catch'
dataobj.o(.text$_ZNSt14__simple_allocISsSt24__default_alloc_templateILb1ELi0EEE1
0deallocateEPSsj+0x1d):dataobj.cpp: undefined reference to
`std::__default_alloc
_template<(bool)1, (int)0>::deallocate(void*, unsigned)'
dataobj.o(.text$_ZSt8_DestroyISsEvPT_+0xd):dataobj.cpp: undefined
reference to `
::~basic_s
tring [in-charge]()'
dataobj.o(.text$_ZNSt14__simple_allocISsSt24__default_alloc_templateILb1ELi0EEE8
allocateEj+0x16):dataobj.cpp: undefined reference to
`std::__default_alloc_templ
ate<(bool)1, (int)0>::allocate(unsigned)'
dataobj.o(.text$_ZSt24__uninitialized_copy_auxIN9__gnu_cxx17__normal_iteratorIPS
sSt6vectorISsSaISsEEEES6_ET0_T_S8_S7_12__false_type+0x73):dataobj.cpp:
undefined
reference to `___cxa_begin_catch'
dataobj.o(.text$_ZSt24__uninitialized_copy_auxIN9__gnu_cxx17__normal_iteratorIPS
sSt6vectorISsSaISsEEEES6_ET0_T_S8_S7_12__false_type+0x8a):dataobj.cpp:
undefined
reference to `___cxa_rethrow'
dataobj.o(.text$_ZSt24__uninitialized_copy_auxIN9__gnu_cxx17__normal_iteratorIPS
sSt6vectorISsSaISsEEEES6_ET0_T_S8_S7_12__false_type+0x95):dataobj.cpp:
undefined
reference to `___cxa_end_catch'
dataobj.o(.text$_ZSt15__copy_backwardIPSsS0_ET0_T_S2_S1_St26random_access_iterat
or_tag+0x36):dataobj.cpp: undefined reference to
`std::basic_string<char, std::c
::eek:perator=(std::basic_string<char, std:
:char_traits<char>, std::allocator<char> > const&)'
dataobj.o(.eh_frame+0x12):dataobj.cpp: undefined reference to
`___gxx_personalit
y_v0'
NN.o(.text+0x1d):NN.cpp: undefined reference to
`std::ios_base::Init::Init[in-ch
arge]()'
NN.o(.text+0x6e):NN.cpp: undefined reference to
`std::ios_base::Init::~Init [in-
charge]()'
NN.o(.eh_frame+0x11):NN.cpp: undefined reference to `___gxx_personality_v0'
tex.o(.text+0x1d4):tex.cpp: undefined reference to `operator
new[](unsigned)'
tex.o(.text+0x2cc):tex.cpp: undefined reference to `operator
new[](unsigned)'
tex.o(.text+0x303):tex.cpp: undefined reference to `operator
new[](unsigned)'
tex.o(.text+0x37d):tex.cpp: undefined reference to `operator
new[](unsigned)'
tex.o(.text+0x3fe):tex.cpp: undefined reference to `operator
new[](unsigned)'
tex.o(.text+0x435):tex.cpp: more undefined references to `operator
new[](unsigne
d)' follow
tex.o(.text+0xa99):tex.cpp: undefined reference to
`std::ios_base::Init::Init[in
-charge]()'
tex.o(.text+0xab4):tex.cpp: undefined reference to
`std::ios_base::Init::~Init [
in-charge]()'
tex.o(.text$_ZN4Loki11SmallObjectINS_14SingleThreadedELj4096ELj64EEdlEPvj+0x28):
tex.cpp: undefined reference to
`Loki::SmallObjAllocator::Deallocate(void*, unsi
gned)'
tex.o(.data$_ZTI7letter_+0x0):tex.cpp: undefined reference to `vtable
for __cxxa
biv1::__si_class_type_info'
tex.o(.data$_ZTIN4Loki11SmallObjectINS_14SingleThreadedELj4096ELj64EEE+0x0):tex.
cpp: undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
tex.o(.data$_ZTIN4Loki14SingleThreadedINS_11SmallObjectIS0_Lj4096ELj64EEEEE+0x0)
:tex.cpp: undefined reference to `vtable for __cxxabiv1::__class_type_info'
tex.o(.text$_ZN4Loki15SingletonHolderINS_11SmallObjectINS_14SingleThreadedELj409
6ELj64EE19MySmallObjAllocatorENS_12CreateStaticENS_16PhoenixSingletonES2_E16Dest
roySingletonEv+0x27):tex.cpp: undefined reference to `__assert'
tex.o(.text$_ZN4Loki11SmallObjectINS_14SingleThreadedELj4096ELj64EE19MySmallObjA
llocatorC1Ev+0x1d):tex.cpp: undefined reference to
`Loki::SmallObjAllocator::Sma
llObjAllocator[not-in-charge](unsigned, unsigned)'
tex.o(.text$_ZSt8_DestroyIN4Loki14FixedAllocatorEEvPT_+0xd):tex.cpp:
undefined r
eference to `Loki::FixedAllocator::~FixedAllocator [in-charge]()'
tex.o(.text$_ZNSt14__simple_allocIN4Loki14FixedAllocatorESt24__default_alloc_tem
plateILb1ELi0EEE10deallocateEPS1_j+0x24):tex.cpp: undefined reference to
`std::_
_default_alloc_template<(bool)1, (int)0>::deallocate(void*, unsigned)'
tex.o(.eh_frame+0x12):tex.cpp: undefined reference to
`___gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [all] Error 1
 
V

Victor Bazarov

klaas said:
Can I put implementation and header class definition in separate files
also for templatized classes?

Usually not. The definition has to be visible to the compiler
at the point of instantiation of the template. Read more about
keyword "export".

Victor
 
S

Samuele Armondi

klaas said:
Can I put implementation and header class definition in separate files
also for templatized classes?
I get a lot of errors and i don't know why.
AFAIK, template definitions and declarations need to be in the same
translation unit (header file or cpp file). In fact, the STL is all in the
headers, there are no object files or anything.
HTH,
S. Armondi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top