C++ Undefined symbols for architecture x86_64 with user defined function

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:38:43

问题


I'm getting the following error message at compilation.

    Undefined symbols for architecture x86_64:
  "LSMDP2<HC_Base, HC_Base, Sine>::LSMDP2(int, int, std::unique_ptr<Forward, std::default_delete<Forward> >&, std::unique_ptr<BrownianMotionIncGenerator, std::default_delete<BrownianMotionIncGenerator> >&, std::unique_ptr<Driver, std::default_delete<Driver> >&, Sine&&, gsl_vector*, std::unique_ptr<Regression, std::default_delete<Regression> >&, std::unique_ptr<UniformSims, std::default_delete<UniformSims> >&, std::unique_ptr<TimeGrid, std::default_delete<TimeGrid> >&, HC_Base&&, HC_Base&&)", referenced from:
      LSMDP2<HC_Base, HC_Base, Sine> make_LSMDP2<HC_Base, HC_Base, Sine>(int, int, std::unique_ptr<Forward, std::default_delete<Forward> >&, std::unique_ptr<BrownianMotionIncGenerator, std::default_delete<BrownianMotionIncGenerator> >&, std::unique_ptr<Driver, std::default_delete<Driver> >&, Sine&&, gsl_vector*, std::unique_ptr<Regression, std::default_delete<Regression> >&, std::unique_ptr<UniformSims, std::default_delete<UniformSims> >&, std::unique_ptr<TimeGrid, std::default_delete<TimeGrid> >&, HC_Base&&, HC_Base&&) in main.o


ld: symbol(s) not found for architecture x86_64

collect2: error: ld returned 1 exit status

make: *** [BSDESimV2] Error 1

The symbol which is supposedly undefined is

template<class BasisY, class BasisZ, class Term>  LSMDP2<BasisY,BasisZ,Term> make_LSMDP2(int N, int q , F_Forward_  & F  ,
         BM_Inc_ & B,   B_Driver_ & D,  Term && Ter,  gsl_vector * X0 ,
        A_Regression_& R, A_UniSims_ & M, T_Grid_ & T /*Time grid initializer*/, BasisY && BY, BasisZ && BZ){
return LSMDP2<BasisY,BasisZ,Term>{N, q ,F, B, D, std::forward<Term>(Ter), X0, R, M , T, std::forward<BasisY>(BY), std::forward<BasisZ>( BZ) };
}

This function is contained in the header file in which the LSMDP2 class is defined. I've checked the linking, it's fine, and the constructor 'LSMPD2(....)' is defined in a separate cpp file. I've made sure not to do the usual mistakes, like screw up linking, forget to build the cpp file, forgot defined the constructor, or use gcc rather than g++.

Actually, I tried the same construction on a much simpler example - only one cpp file in the compilation, fewer arguments, but both templates and unique_ptrs in use - and it worked fine. Does anyone know what could be causing this problem?


回答1:


You say the LSMPD2 constructor is "defined in a separate cpp file." That's your problem. Since the class is apparently a template, all methods of the class which are used outside must be defined in the header file, not a separately-compiled implementation file.

For more details, see here: Why can templates only be implemented in the header file?



来源:https://stackoverflow.com/questions/19161371/c-undefined-symbols-for-architecture-x86-64-with-user-defined-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!