Search code examples
c++circular-dependencyundeclared-identifier

Trouble coming up with a solution to a circular reference in C++


I have two base classes, valuationFunction and SimulationEngine where I believe I have created a circular reference that I'm not sure how to sort out. Originally I only had SimulationEngine include ValuationFunction, but then I created the enum RiskFactor in SimulationEngine that I need ValuationFunction to recognize, and that's where I think my trouble stems from. Just looking at this it obviously seems wrong since the two header filers are including each other, but how else would I get the ValuationFunction (and all it's inherited classes) to be able to take an object of type RiskFactor as input?

SimulationEngine :

#pragma once
#define SIMULATION_ENGINE_H
#include "valuationFunction.h"
#include "Wrapper.h"
class SimulationEngine
{
public:
    enum RiskFactor { interest_rate, equity, volatility, FX_rate };
    SimulationEngine(double horizon, Wrapper<valuationFunction> theFunction_, RiskFactor simulatedRiskFactor);
    virtual void DoOnePath(double vol, double normvariate) = 0;
    virtual SimulationEngine* clone() const = 0;
    const virtual double GetHorizon();
    Wrapper<valuationFunction>& GetFunction();
    RiskFactor simulatedRiskFactor;
protected:
    double horizon;
    Wrapper<valuationFunction> theFunction;
};

ValuationFunction:

#pragma once
#define VALUATION_FUNCTION_H
#include "SimulationEngine.h"
class valuationFunction
{
public:
    valuationFunction(double TTM);
    virtual void ValueInstrument() = 0;
    virtual double GetValue() const;
    virtual void RiskFactorAdd(double increment, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
    virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
    virtual void UpdateTTM(double timeStep);
    virtual valuationFunction* clone() const = 0;
    virtual ~valuationFunction() {}
private:

protected:
    double f;
    double TTM;
};

Solution

  • Create RiskFactor.h. Stick the enum in there. Include it in both headers.