I am having troubles with the circular dependency between the classes A,B,C. The user type cfunction from class A points to the static method of C::F1. Here is the code:
File A.h
#ifndef A_H
#define A_H
#include "C.h"
class C;
template <typename T> using cfunction = T(*)(T, T);
template <typename T>
class A{
public:
cfunction <T> X;
A () : X(&C::F1) {}
A (const cfunction <T> &pX ) : X(pX){};
virtual ~A() = 0;
};
#endif
File B.h
#ifndef B_H
#define B_H
#include "A.h"
template <typename T>
class B : public A <T> {
public:
B() : A<T>(), b(0.0) {}
B(const T b_, const cfunction <T> &pX ) : A <T>(pX), b(b_){}
virtual ~B() {};
};
#endif
Finally, in the method init() of C a shared pointer to A is stored. The method F1 calls F2 with the template parameter F3. Here is the code:
File C.h
#ifndef C_H
#define C_H
#include "A.h"
template <typename T>
class A;
#include <memory>
#include <list>
template <typename T>
using List = std::list<std::shared_ptr<A<T> > >;
//Definition of all projections
class C {
public:
template <typename T, typename Function> static T F2(Function f, const T a, const T b);
template <typename T> static void init(List<T> l);
template <typename T> static T F1(const T a, const T b);
template <typename T> static T F3(const T a, const T b);
};
#include "C.hpp"
#endif
File C.hpp
#ifndef C_HPP
#define C_HPP
#include "B.h"
template <typename T>
class B;
template <typename T, typename Function>
T C::F2(Function f, const T a, const T b) { return f(a, b);}
template <typename T> void C::init(List<T> l) {
auto test = std::make_shared <B < T >> (0.0, F1<T>);
l.push_back(test);
}
template <typename T> T C::F1(const T a, const T b) { return F2(F3<T>, a, b);}
template <typename T> T C::F3(const T a, const T b) {return a + b;}
#endif
The main file: main.cpp
#include "C.h"
int main(){
List <double> l;
C::init(l);
return 0;
}
Sorry for the slightly complicated code. A simpler version of the code works well, but this "full" variant strikes. I am not able to fix the problem for g++; compile options: -std=c++11.
Thanks for your help...