共享指针多态对象的引用未正确传递

时间:2016-02-12 05:14:07

标签: oop c++11 polymorphism smart-pointers

我有一个程序,主要按以下方式调用Simulation类对象。

int number_of_sims = std::stoi(num_sims)/MAX_THREADS_SIM_THREADS;
for (int inst=0; inst<Instruments.size(); inst++)
    {
        Simulation Simulations(Instruments[inst], Symbols[inst], number_of_sims);
        SimVector.push_back(Simulations);
    }

模拟类反过来调用类Analysis和类策略,并具有以下骨架。

class Simulation
{
public:
    Simulation();
    Simulation(Instrument& instrument, String& Symbol, int runs);
    virtual ~Simulation();
    boost::shared_ptr<Strategy> getStrategyName(String& StratName);
    bool constructTimeSeries();
    bool InitializeStrategy();
    bool getMeanMTM();
    bool printStrategyTrades(int run);
    bool printAggregateMTM();
    bool Run();
private:
    String Symbol_;
    double Mean_;
    int runs_;
    Instrument instrument_;
    std::map<String, DoubleVector> AggregateMTM_;
    boost::shared_ptr<Analysis> analysis_;
    boost::shared_ptr<Strategy> strategy_;
};

构造函数看起来像这样。

Simulation::Simulation(Instrument& instrument, String& Symbol, int runs) 
: instrument_(instrument), Symbol_(Symbol), analysis_(new Analysis(instrument)), runs_(runs)
{
    String filelocation = "/else/abc/xyz/klmn/file.config";
    ConfigReader Config(filelocation, "=", false);
    Config.parseFile();
    String StrategyName = Config.getValue("strategyname");
    Logger::getLogger().log(DEBUG, "The Strategy name is: " + StrategyName);
    strategy_ = getStrategyName(StrategyName);
}

现在我在配置文件中对XYZ策略进行多态调用,如下所示。

boost::shared_ptr<Strategy> Simulation::getStrategyName(String& StratName)
{
    if (StratName.compare("XYZ") == 0)
        return (boost::shared_ptr<Strategy>(new XYZ(instrument_, Symbol_)));  
}

我的XYZ策略类继承策略库并填充TS_(LongStruct向量)成员变量,该变量是类策略的受保护成员,并使用它来执行某些功能,其中一个功能是获取向量的大小。

这两个类的骨架如下所示。

class Strategy
{
public:
    Strategy(Instrument& instrument, String& Symbol);
    virtual ~Strategy();
    virtual bool Initialize();
    virtual bool printData(std::ofstream& outputfile);
    virtual double getMeanMTM();

protected:
    virtual int CalculateTradeSignal(double& lower_limit, double& upper_limit, double& tau, double& meanTau, bool trade);
    virtual double CalculateMTM(double& EntryPrice, double& ExitPrice);
    std::map<int, String> Side_; 
    std::vector<String> TradeVector_;
    Instrument instrument_;
    LongToSymbolInfoPairVector TS_;
    String Symbol_, start_, end_, tradeend_;
    int numticks_, tradeticks_, tickinterval_;
    double MeanMTM_;
};

XYZ:

class XYZ : public Strategy 
{
public:
    XYZ(Instrument& instrument, String& Symbol);
    virtual ~XYZ();
    bool Calculate(); 
    bool CalculateTau();
    bool Initialize();
    bool updateNetPosition(int SigVal, int i, double& tau);
    double getMeanMTM();
    bool printData(std::ofstream& outputfile);

private:
    // Initializers
    bool entry_, trade_;
    int netposition_;
    long unsigned int tauMA_, lokBK_;
    long entryT_, exitT_;
    // TODO: Move the temporary variables, entryP, exitP, MTM to local variables 
    double entryP_, exitP_, MTM_, stoploss_, takeprofit_;
    double lowtau_, hightau_;    
    LongVector entrytimeVector_, exittimeVector_;
    IntVector posVector_;
    DoubleVector entryPriceVector_, exitPriceVector_, MTMVector_, tauVector_;
};

在CalculateTau函数中,我尝试获取已按以下方式初始化的TS_向量的大小。

bool XYZ::CalculateTau()
    {
        Logger::getLogger().log(DEBUG, "Calculating Tau  .. ");
        Logger::getLogger().log(DEBUG, "The size of the TS is: " + std::to_string(TS_.size()));
        return true;
    }

Strategy::Strategy(Instrument& instrument, String& Symbol) 
: instrument_(instrument)
{

}

bool Strategy::Initialize()
{
    TS_ = instrument_.GetTS();
return true;
}

这里让TS做以下事情:

class Instrument
{
    public:
    Instrument();
    virtual ~Instrument();
    LongToSymbolInfoPairVector GetTS() { return newTS_; }

private:
    LongToSymbolInfoPairVector TS_, newTS_;

};

现在问题是我得到这个大小的值为0。

请帮助我告诉我,我在哪里错了。

1 个答案:

答案 0 :(得分:0)

我已经能够通过减少来自Simulation-&gt; Analysis-&gt; Instrument to Instrument-&gt; Simulation-&gt; Analysis in Analysis-&gt; Strategy

的间接来解决这个问题