template separation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • klaas

    #1

    template separation

    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+0x 0):bprop.cpp: multiple definition of `psyche::debug'
    psyche.o(.bss+0 x0):psyche.cpp: first defined here
    NN.o(.bss+0x0): NN.cpp: multiple definition of `psyche::debug'
    psyche.o(.bss+0 x0):psyche.cpp: first defined here
    psyche.o(.text+ 0x5f):psyche.cp p: undefined reference to
    `psyche::percep tron<floa
    t>::perceptron[in-charge]()'
    psyche.o(.text+ 0xc6):psyche.cp p: undefined reference to `std::cout'
    psyche.o(.text+ 0xd3):psyche.cp p: undefined reference to
    `std::basic_ost ream<char
    , std::char_trait s<char> >& std::operator<< <std::char_trai ts<char>[color=blue]
    >(std::basic[/color]
    _ostream<char, std::char_trait s<char> >&, char const*)'
    psyche.o(.text+ 0xde):psyche.cp p: undefined reference to
    `std::basic_ost ream<char
    , std::char_trait s<char> >& std::endl<char, std::char_trait s<char>[color=blue]
    >(std::basic_[/color]
    ostream<char, std::char_trait s<char> >&)'
    psyche.o(.text+ 0xe3):psyche.cp p: undefined reference to
    `std::basic_ost ream<char
    , std::char_trait s<char> >::operator<<(s td::basic_ostre am<char,
    std::char_trait s
    <char> >& (*)(std::basic_ ostream<char, std::char_trait s<char> >&))'
    psyche.o(.text+ 0x13a):psyche.c pp: undefined reference to
    `psyche::backpr op<float
    , psyche::data, psyche::sigmoid >::offsets(std: :vector<int,
    std::allocator< int> >
    &)'
    psyche.o(.text+ 0x149):psyche.c pp: undefined reference to `std::cout'
    psyche.o(.text+ 0x156):psyche.c pp: undefined reference to
    `std::basic_ost ream<cha
    r, std::char_trait s<char> >& std::operator<< <std::char_trai ts<char>[color=blue]
    >(std::basi[/color]
    c_ostream<char, std::char_trait s<char> >&, char const*)'
    psyche.o(.text+ 0x16a):psyche.c pp: undefined reference to
    `std::basic_ost ream<cha
    r, std::char_trait s<char> >& std::operator<< <std::char_trai ts<char>[color=blue]
    >(std::basi[/color]
    c_ostream<char, std::char_trait s<char> >&, char const*)'
    psyche.o(.text+ 0x175):psyche.c pp: undefined reference to
    `std::basic_ost ream<cha
    r, std::char_trait s<char> >& std::endl<char, std::char_trait s<char>[color=blue]
    >(std::basic[/color]
    _ostream<char, std::char_trait s<char> >&)'
    psyche.o(.text+ 0x17a):psyche.c pp: undefined reference to
    `std::basic_ost ream<cha
    r, std::char_trait s<char> >::operator<<(s td::basic_ostre am<char,
    std::char_trait
    s<char> >& (*)(std::basic_ ostream<char, std::char_trait s<char> >&))'
    psyche.o(.text+ 0x188):psyche.c pp: undefined reference to
    `std::allocator <char>::
    allocator[in-charge]()'
    psyche.o(.text+ 0x1a9):psyche.c pp: undefined reference to
    `std::basic_str ing<char
    , std::char_trait s<char>, std::allocator< char>[color=blue]
    >::basic_stri ng[in-charge](char c[/color]
    onst*, std::allocator< char> const&)'
    psyche.o(.text+ 0x1bb):psyche.c pp: undefined reference to
    `psyche::percep tron<flo
    at>::file(std:: basic_string<ch ar, std::char_trait s<char>,
    std::allocator< char> >
    )'
    psyche.o(.text+ 0x1d4):psyche.c pp: undefined reference to
    `std::basic_str ing<char
    , std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()'
    psyche.o(.text+ 0x1e7):psyche.c pp: undefined reference to
    `std::basic_str ing<char
    , std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()'
    psyche.o(.text+ 0x203):psyche.c pp: undefined reference to
    `std::allocator <char>::
    ~allocator [in-charge]()'
    psyche.o(.text+ 0x219):psyche.c pp: undefined reference to
    `std::allocator <char>::
    ~allocator [in-charge]()'
    psyche.o(.text+ 0x2a7):psyche.c pp: undefined reference to
    `std::ios_base: :Init::I
    nit[in-charge]()'
    psyche.o(.text+ 0x2f8):psyche.c pp: undefined reference to
    `std::ios_base: :Init::~
    Init [in-charge]()'
    psyche.o(.text$ _ZNSt6vectorIiS aIiEE13_M_inser t_auxEN9__gnu_c xx17__normal_it erato
    rIPiS1_EERKi+0x 1a5):psyche.cpp : undefined reference to `___cxa_begin_c atch'
    psyche.o(.text$ _ZNSt6vectorIiS aIiEE13_M_inser t_auxEN9__gnu_c xx17__normal_it erato
    rIPiS1_EERKi+0x 1e1):psyche.cpp : undefined reference to `___cxa_rethrow '
    psyche.o(.text$ _ZNSt6vectorIiS aIiEE13_M_inser t_auxEN9__gnu_c xx17__normal_it erato
    rIPiS1_EERKi+0x 1ec):psyche.cpp : undefined reference to `___cxa_end_cat ch'
    psyche.o(.text$ _ZNSt14__simple _allocIiSt24__d efault_alloc_te mplateILb1ELi0E EE10d
    eallocateEPij+0 x1d):psyche.cpp : undefined reference to
    `std::__default _alloc_tem
    plate<(bool)1, (int)0>::deallo cate(void*, unsigned)'
    psyche.o(.text$ _ZNSt14__simple _allocIiSt24__d efault_alloc_te mplateILb1ELi0E EE8al
    locateEj+0x16): psyche.cpp: undefined reference to
    `std::__default _alloc_template
    <(bool)1, (int)0>::alloca te(unsigned)'
    psyche.o(.text$ _ZNSt14__simple _allocISt6vecto rIiSaIiEESt24__ default_alloc_t empla
    teILb1ELi0EEE10 deallocateEPS2_ j+0x23):psyche. cpp: undefined reference to
    `std::_
    _default_alloc_ template<(bool) 1, (int)0>::deallo cate(void*, unsigned)'
    psyche.o(.text$ _ZSt8_DestroyIN 6psyche6matrixI dEEEvPT_+0xd):p syche.cpp:
    undefined
    reference to `psyche::matrix <double>::~matr ix [in-charge]()'
    psyche.o(.text$ _ZNSt14__simple _allocIN6psyche 6matrixIdEESt24 __default_alloc _temp
    lateILb1ELi0EEE 10deallocateEPS 2_j+0x24):psych e.cpp: undefined reference
    to `std:
    :__default_allo c_template<(boo l)1, (int)0>::deallo cate(void*, unsigned)'
    psyche.o(.text$ _ZSt8_DestroyIN 6psyche6matrixI fEEEvPT_+0xd):p syche.cpp:
    undefined
    reference to `psyche::matrix <float>::~matri x [in-charge]()'
    psyche.o(.text$ _ZNSt14__simple _allocIN6psyche 6matrixIfEESt24 __default_alloc _temp
    lateILb1ELi0EEE 10deallocateEPS 2_j+0x24):psych e.cpp: undefined reference
    to `std:
    :__default_allo c_template<(boo l)1, (int)0>::deallo cate(void*, unsigned)'
    psyche.o(.eh_fr ame+0x12):psych e.cpp: undefined reference to
    `___gxx_persona lity_
    v0'
    matrix.o(.text+ 0x1d):matrix.cp p: undefined reference to
    `std::ios_base: :Init::In
    it[in-charge]()'
    matrix.o(.text+ 0x38):matrix.cp p: undefined reference to
    `std::ios_base: :Init::~I
    nit [in-charge]()'
    matrix.o(.eh_fr ame+0x11):matri x.cpp: undefined reference to
    `___gxx_persona lity_
    v0'
    test.o(.text+0x 1d):test.cpp: undefined reference to
    `std::ios_base: :Init::Init[i
    n-charge]()'
    test.o(.text+0x 38):test.cpp: undefined reference to
    `std::ios_base: :Init::~Init
    [in-charge]()'
    test.o(.eh_fram e+0x11):test.cp p: undefined reference to
    `___gxx_persona lity_v0'
    bprop.o(.text+0 x1d):bprop.cpp: undefined reference to
    `std::ios_base: :Init::Init
    [in-charge]()'
    bprop.o(.text+0 x6e):bprop.cpp: undefined reference to
    `std::ios_base: :Init::~Ini
    t [in-charge]()'
    bprop.o(.text$_ ZN6psyche8backp ropIdNS_4dataEN S_7sigmoidEEC1E v+0xe):bprop.cp p:
    un
    defined reference to `psyche::topolo gy<double>::top ology[not-in-charge]()'
    bprop.o(.text$_ ZN6psyche8backp ropIdNS_4dataEN S_7sigmoidEEC1E v+0x1c):bprop.c pp:
    u
    ndefined reference to `psyche::data<d ouble>::data[not-in-charge]()'
    bprop.o(.text$_ ZN6psyche8backp ropIdNS_4dataEN S_7sigmoidEEC1E v+0x27):bprop.c pp:
    u
    ndefined reference to `psyche::sigmoi d<double>::sigm oid[not-in-charge]()'
    bprop.o(.eh_fra me+0x12):bprop. cpp: undefined reference to
    `___gxx_persona lity_v0
    '
    topology.o(.tex t+0x1d):topolog y.cpp: undefined reference to
    `std::ios_base: :Init
    ::Init[in-charge]()'
    topology.o(.tex t+0x38):topolog y.cpp: undefined reference to
    `std::ios_base: :Init
    ::~Init [in-charge]()'
    topology.o(.eh_ frame+0x11):top ology.cpp: undefined reference to
    `___gxx_persona l
    ity_v0'
    dataobj.o(.text +0xe1):dataobj. cpp: undefined reference to
    `std::basic_str ing<cha
    r, std::char_trait s<char>, std::allocator< char> >::length() const'
    dataobj.o(.text +0xfd):dataobj. cpp: undefined reference to
    `std::basic_str ing<cha
    r, std::char_trait s<char>, std::allocator< char> >::operator[](unsigned)
    const'
    dataobj.o(.text +0x12b):dataobj .cpp: undefined reference to
    `std::basic_str ing<ch
    ar, std::char_trait s<char>, std::allocator< char> >::substr(unsig ned,
    unsigned) c
    onst'
    dataobj.o(.text +0x153):dataobj .cpp: undefined reference to
    `std::basic_str ing<ch
    ar, std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()'

    dataobj.o(.text +0x163):dataobj .cpp: undefined reference to
    `std::basic_str ing<ch
    ar, std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()'

    dataobj.o(.text $_ZSteqIcSt11ch ar_traitsIcESaI cEEbRKSbIT_T0_T 1_ES8_+0x14):da taobj
    ..cpp: undefined reference to `std::basic_str ing<char,
    std::char_trait s<char>, st
    d::allocator<ch ar> >::compare(std: :basic_string<c har,
    std::char_trait s<char>, st
    d::allocator<ch ar> > const&) const'
    dataobj.o(.text $_ZSt10_Constru ctISsSsEvPT_RKT 0_+0x42):dataob j.cpp:
    undefined ref
    erence to `std::basic_str ing<char, std::char_trait s<char>,
    std::allocator< char>[color=blue]
    >::basic_stri ng[in-charge](std::basic_str ing<char,[/color]
    std::char_trait s<char>, std::
    allocator<char> > const&)'
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0x49):dataob j.cpp: undefined reference to
    `std::basic_str ing<c
    har, std::char_trait s<char>, std::allocator< char>[color=blue]
    >::basic_stri ng[in-charge](std[/color]
    ::basic_string< char, std::char_trait s<char>, std::allocator< char> > const&)'
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0xb8):dataob j.cpp: undefined reference to
    `std::basic_str ing<c
    har, std::char_trait s<char>, std::allocator< char>[color=blue]
    >::operator=(st d::basic_string[/color]
    <char, std::char_trait s<char>, std::allocator< char> > const&)'
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0xcb):dataob j.cpp: undefined reference to
    `std::basic_str ing<c
    har, std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()
    '
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0xe4):dataob j.cpp: undefined reference to
    `std::basic_str ing<c
    har, std::char_trait s<char>, std::allocator< char> >::~basic_strin g
    [in-charge]()
    '
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0x1e3):datao bj.cpp: undefined reference to
    `___cxa_begin_c atch
    '
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0x21f):datao bj.cpp: undefined reference to
    `___cxa_rethrow '
    dataobj.o(.text $_ZNSt6vectorIS sSaISsEE13_M_in sert_auxEN9__gn u_cxx17__normal _iter
    atorIPSsS1_EERK Ss+0x22a):datao bj.cpp: undefined reference to
    `___cxa_end_cat ch'
    dataobj.o(.text $_ZNSt14__simpl e_allocISsSt24_ _default_alloc_ templateILb1ELi 0EEE1
    0deallocateEPSs j+0x1d):dataobj .cpp: undefined reference to
    `std::__default _alloc
    _template<(bool )1, (int)0>::deallo cate(void*, unsigned)'
    dataobj.o(.text $_ZSt8_DestroyI SsEvPT_+0xd):da taobj.cpp: undefined
    reference to `
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>[color=blue]
    >::~basic_s[/color]
    tring [in-charge]()'
    dataobj.o(.text $_ZNSt14__simpl e_allocISsSt24_ _default_alloc_ templateILb1ELi 0EEE8
    allocateEj+0x16 ):dataobj.cpp: undefined reference to
    `std::__default _alloc_templ
    ate<(bool)1, (int)0>::alloca te(unsigned)'
    dataobj.o(.text $_ZSt24__uninit ialized_copy_au xIN9__gnu_cxx17 __normal_iterat orIPS
    sSt6vectorISsSa ISsEEEES6_ET0_T _S8_S7_12__fals e_type+0x73):da taobj.cpp:
    undefined
    reference to `___cxa_begin_c atch'
    dataobj.o(.text $_ZSt24__uninit ialized_copy_au xIN9__gnu_cxx17 __normal_iterat orIPS
    sSt6vectorISsSa ISsEEEES6_ET0_T _S8_S7_12__fals e_type+0x8a):da taobj.cpp:
    undefined
    reference to `___cxa_rethrow '
    dataobj.o(.text $_ZSt24__uninit ialized_copy_au xIN9__gnu_cxx17 __normal_iterat orIPS
    sSt6vectorISsSa ISsEEEES6_ET0_T _S8_S7_12__fals e_type+0x95):da taobj.cpp:
    undefined
    reference to `___cxa_end_cat ch'
    dataobj.o(.text $_ZSt15__copy_b ackwardIPSsS0_E T0_T_S2_S1_St26 random_access_i terat
    or_tag+0x36):da taobj.cpp: undefined reference to
    `std::basic_str ing<char, std::c
    har_traits<char >, std::allocator< char>[color=blue]
    >::operator=(st d::basic_string <char, std:[/color]
    :char_traits<ch ar>, std::allocator< char> > const&)'
    dataobj.o(.eh_f rame+0x12):data obj.cpp: undefined reference to
    `___gxx_persona lit
    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_persona lity_v0'
    tex.o(.text+0x1 d4):tex.cpp: undefined reference to `operator
    new[](unsigned)'
    tex.o(.text+0x2 cc):tex.cpp: undefined reference to `operator
    new[](unsigned)'
    tex.o(.text+0x3 03):tex.cpp: undefined reference to `operator
    new[](unsigned)'
    tex.o(.text+0x3 7d):tex.cpp: undefined reference to `operator
    new[](unsigned)'
    tex.o(.text+0x3 fe):tex.cpp: undefined reference to `operator
    new[](unsigned)'
    tex.o(.text+0x4 35):tex.cpp: more undefined references to `operator
    new[](unsigne
    d)' follow
    tex.o(.text+0xa 99):tex.cpp: undefined reference to
    `std::ios_base: :Init::Init[in
    -charge]()'
    tex.o(.text+0xa b4):tex.cpp: undefined reference to
    `std::ios_base: :Init::~Init [
    in-charge]()'
    tex.o(.text$_ZN 4Loki11SmallObj ectINS_14Single ThreadedELj4096 ELj64EEdlEPvj+0 x28):
    tex.cpp: undefined reference to
    `Loki::SmallObj Allocator::Deal locate(void*, unsi
    gned)'
    tex.o(.data$_ZT I7letter_+0x0): tex.cpp: undefined reference to `vtable
    for __cxxa
    biv1::__si_clas s_type_info'
    tex.o(.data$_ZT IN4Loki11SmallO bjectINS_14Sing leThreadedELj40 96ELj64EEE+0x0) :tex.
    cpp: undefined reference to `vtable for __cxxabiv1::__s i_class_type_in fo'
    tex.o(.data$_ZT IN4Loki14Single ThreadedINS_11S mallObjectIS0_L j4096ELj64EEEEE +0x0)
    :tex.cpp: undefined reference to `vtable for __cxxabiv1::__c lass_type_info'
    tex.o(.text$_ZN 4Loki15Singleto nHolderINS_11Sm allObjectINS_14 SingleThreadedE Lj409
    6ELj64EE19MySma llObjAllocatorE NS_12CreateStat icENS_16Phoenix SingletonES2_E1 6Dest
    roySingletonEv+ 0x27):tex.cpp: undefined reference to `__assert'
    tex.o(.text$_ZN 4Loki11SmallObj ectINS_14Single ThreadedELj4096 ELj64EE19MySmal lObjA
    llocatorC1Ev+0x 1d):tex.cpp: undefined reference to
    `Loki::SmallObj Allocator::Sma
    llObjAllocator[not-in-charge](unsigned, unsigned)'
    tex.o(.text$_ZS t8_DestroyIN4Lo ki14FixedAlloca torEEvPT_+0xd): tex.cpp:
    undefined r
    eference to `Loki::FixedAll ocator::~FixedA llocator [in-charge]()'
    tex.o(.text$_ZN St14__simple_al locIN4Loki14Fix edAllocatorESt2 4__default_allo c_tem
    plateILb1ELi0EE E10deallocateEP S1_j+0x24):tex. cpp: undefined reference to
    `std::_
    _default_alloc_ template<(bool) 1, (int)0>::deallo cate(void*, unsigned)'
    tex.o(.eh_frame +0x12):tex.cpp: undefined reference to
    `___gxx_persona lity_v0'
    collect2: ld returned 1 exit status
    make: *** [all] Error 1

  • Victor Bazarov

    #2
    Re: template separation

    "klaas" <jan@zonnet.r u> wrote...[color=blue]
    > Can I put implementation and header class definition in separate files
    > also for templatized classes?[/color]

    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


    Comment

    • Samuele Armondi

      #3
      Re: template separation


      "klaas" <jan@zonnet.r u> wrote in message
      news:MqjLa.259$ w3.144690@zonne t-reader-1...[color=blue]
      > 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.[/color]
      [color=blue]
      >[/color]
      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


      Comment

      Working...