This is a follow-up question from this question. When I instantiate using the 'make_LSMDP2(...)' method as follows
auto A = make_LSMDP2(N,q,F,BM,Dr,Sine{b,c*Time},X0,R,M,T,HC_Base{T->ReturnTerm(),X0->size , length, prewidth},HC_Base{T->ReturnTerm(),X0->size , length, prewidth});
the constructor of the objects of type 'BasisY', 'BasisZ' and 'Term' works fine: I run tests in the constructors of each one to make sure they are working properly. However, when I try to use the instantiated objects, the behaviour is as though they were instantiated with random parameters, so obviously everything is a mess.
Please see this for a simple version of how the above method is implemented.
Does anyone know why this is happening? The only thing I can tell might be the problem is that I define the types 'Sine' and 'HC_Base' in a different header file to the one where I define the template method. The header, however, is included in the file where the template is constructed, so I don't see how this is a problem.
In fact, I tried to isolate the problem by developing a simple example of the 'LSMDP2' template in another project to see if there was something fundamentally wrong with the way I was constructing the class. Everything worked fine in that case. The parameter classes were simpler but used all the same data types (just to make sure that I was using the libraries correctly) as well as non-trivial constructors. The only difference I can tell is that the parameter classes and the template method were defined in the same header in that case.
If you need any further information or compiler details, please let me know!
EDIT: By request, a simplified example of the code that doesn't work: using the constructor
template<class BasisY, class BasisZ, class Term > LSMDP2<BasisY,BasisZ,Term>::LSMDP2(Term && P, BasisY && BY1, BasisZ && BZ1) :
Phi(std::forward<Term>(P)) , BY(std::forward<BasisY>(BY1)), BZ(std::forward<BasisZ>(BZ1)){
BY.func_test();
}
instantiate an object using
auto A = make2_LSMDP2(Sine{b,c*Time},HC_Base{T->ReturnTerm(),X0->size , length, prewidth},HC_Base{T->ReturnTerm(),X0->size , length, prewidth});
where 'make2_LSMDP2(...)' just calls the simpler constructor. The constructors of 'Sine' and HC_Base are as follows:
Sine(gsl_vector * LC,double con){
cout << "Vector dimension = " << LC->size << endl;
LinComb = gsl_vector_alloc(LC->size);
gsl_vector_memcpy(LinComb,LC);
c=con;
}
HC_Base(double T /*Terminal time*/, size_t d/*dimension*/, double width/*cube width*/, double bound/*prewidth*/) : base_f(T), CubeWidth(width),dimension(d), pre_width (bound)
{
func_test();
}
'base_f' is just a parent class wrapper that provides some default behaviour. In the constructor of HC_Base, a test is called which tests the performance of the instantiated object, and this works fine. The same test is called in the LSMDP2 constructor on the same object, showing entirely different behaviour! In fact, I've only shown the test on HC_Base in the constructor of LSMDP2, but I can perform similar tests with Sine that will fail.