实现C++类方法,它可以绘制多个其他非继承类?

实现C++类方法,它可以绘制多个其他非继承类?,c++,class,c++11,inner-classes,forward-declaration,C++,Class,C++11,Inner Classes,Forward Declaration,所以,我将尽最大努力定义我的问题,请容忍我。我正在从事一项基本的财务工作,其中我为不同的基类制作了多个头文件:这些是资产、头寸和投资组合。Assets是我的基类,Position类包含通过远期声明对Assets对象的引用,Portfolio类包含通过远期声明对Position类的类似引用 现在,我正在尝试在Portfolio中创建一个名为getPortfolioValue的方法,该方法将根据该对象是否是资产的派生类之一(即权益、优先股和债券)返回投资组合头寸对象的市场价值 每种方法都有一个get

所以,我将尽最大努力定义我的问题,请容忍我。我正在从事一项基本的财务工作,其中我为不同的基类制作了多个头文件:这些是资产、头寸和投资组合。Assets是我的基类,Position类包含通过远期声明对Assets对象的引用,Portfolio类包含通过远期声明对Position类的类似引用

现在,我正在尝试在Portfolio中创建一个名为getPortfolioValue的方法,该方法将根据该对象是否是资产的派生类之一(即权益、优先股和债券)返回投资组合头寸对象的市场价值

每种方法都有一个getMarketValue方法,该方法被调用,根据继承的getter getAssetType告诉我它们的资产类型,该方法实际生成数字答案

getPortfolioValue在Portfolio.hpp中定义,并在Position.hpp中实现,因为向前声明,但会给出各种不完整的类型错误。有问题的部分位于第二个到最后一个code Position.hpp的底部

我试着尽可能地给你最干净的衣服。有人知道如何使这个getPortfolioValue方法在必须跨越2个头文件的情况下工作吗?提前非常感谢你

错误图片:

这是我的密码:

资产净值

Position.hpp

投资组合.hpp


问题是您需要执行ifposition->getAssetBase->getAssetType==Equity,而不是ifposition->getAssetBase->getAssetType=Equity


希望这能有所帮助。

演示问题的内联方法取决于asset.h中的定义,但在定义问题函数时,您尚未包括asset.h。我不确定在问题函数之前仅仅包含asset.h是否可以解决问题,这里可能存在循环依赖关系,我看不到。

您的包含不是基于类的依赖关系,这是主要问题的根源

从Assets.hpp中删除这些行,因为资产、股权、优先股和债券类别不取决于头寸和投资组合:

这里有一个语法错误remove:并且也是过时的;类似函数之后:

Asset(a, b, c, d) 
{                       
}:
从Position.hpp中删除此行,因为头寸不取决于投资组合:

还将Portfolio::getPortfolioValue的定义移动到它所属的Portfolio.hpp中:

class Portfolio {
public:
    double getPortfolioValue(const int& n) {
        if (position->getAssetBase()->getAssetType() == "EQUITY") {
            return position->getAssetBase()->getMarketValue(const int& n);
        }

        else if (position->getAssetBase()->getAssetType() == "PREFERRED") {
            return position->getAssetBase()->getMarketValue(const int& n);
        }

        else if (position->getAssetBase()->getAssetType() == "BOND"){
            return position->getAssetBase()->getMarketValue(const int& n);
        }
    }
    /* other stuff omitted for brevity */
};
现在,您的投资组合类别取决于头寸和资产,因此您必须在Portfolio.hpp中包含它们的标题:

同时从Portfolio.hpp中删除远期申报:

当您调用Position方法时,forward声明没有任何用处。你需要完整的声明

这将修复编译错误


为什么在getPortfolioValue中有这些ifs仍然很奇怪。您在所有这些分支中都在做相同的操作…

您好,我修复了那个幼稚的错误,感谢您的捕获,但我的基本问题仍然存在。我的位置只是一个位置对象,基本上是我拥有多少资产以及使用多长时间。从某种意义上说,公文包只是持有多个头寸。@Coolio2654我看不出有任何错误。你能添加错误吗?为什么要使用那些if语句继承的目的是为了避免这种情况。那些if语句可能确实是不必要的,但我不确定我还能怎么做。返回->。。。代码位也返回相同的错误,即使我以某种方式去掉了if语句。你的include看起来很奇怪。Assets.hpp与Position.hpp和Portfolio.hpp没有依赖关系,因此应删除它们。此外,Position类对Portfolio没有依赖关系,因此应该从Position.hpp中删除include。将Portfolio::getPortfolioValue的定义放入它所属的Portfolio.hpp中。在Portfolio.hpp中,仅包含Position.hpp,这是它唯一的依赖项;“这是一种糟糕的做法,@zett42如果你需要我的推理,我在使用前向声明时接受了上面答案的建议。我希望这会有所帮助。我在顶部的资产标题中包含了后两个标题,并在后两个标题中向前声明了必要的类。我知道这个方法适用于简单的前向声明,但似乎不适用于我更复杂的使用。我接受了你的建议,是的,我的if语句也是不必要的,并实现了它,但现在我有一个更奇怪的错误。这意味着什么?@Coolio2654您还没有实现getMarketValue。您的建议帮助我很好地解决了这个问题。我可以看到,我在前向声明上绊倒了自己,而事实上它们是不充分的,并且使用变通方法让它们工作,给我已经相当大的代码增加了更多容易出错的复杂性。谢谢你指出我的简单错误。
#ifndef Position_hpp
#define Position_hpp
#include <stdio.h>
#include <fstream>
#include <cmath>
#include <string>
#include "Portfolio.hpp"

using namespace std;

class Asset;

class Position{
private:
    Asset* base;
    int position_size;
    double cost_basis;
    double position_age;

public:
    Position(Asset* a, const int& b = 0.0, const double& c = 0.0,
             const double& d = 0.0) :
    base(a), position_size(b), cost_basis(c), position_age(d)              
    {                                                                      
    };

    Asset* getAssetBase() { return base;}      //Getter
    void setAssetBase(Asset* b) { base = b;}   //Setter

};

//  ****************PROBLEM RIGHT BELOW HERE********************************

inline double Portfolio::getPortfolioValue(const int& n) {
if (position->getAssetBase()->getAssetType() == "EQUITY") {
    return position->getAssetBase()->getMarketValue(const int& n);
}

else if (position->getAssetBase()->getAssetType() == "PREFERRED") {
    return position->getAssetBase()->getMarketValue(const int& n);
}

else if (position->getAssetBase()->getAssetType() == "BOND"){
    return position->getAssetBase()->getMarketValue(const int& n);
    }
}
#endif /* Position_hpp */
#ifndef Portfolio_hpp
#define Portfolio_hpp
#include <stdio.h>
#include <fstream>
#include <string>
#include <cmath>

class Position;

class Portfolio {
private:
    Position* position;
    int num_positions;

public:
    Portfolio(Position* a, const int& b) : position(a), num_positions(b)
    {
    };

    Portfolio(const Portfolio&);

    Position* getPosition() { return position;}     //Getter

    double getPortfolioValue(const int& n); //Both of these implemented in Position header.
    double PortolioCostBasis();


};

#endif /* Portfolio_hpp */
#include "Position.hpp"
#include "Portfolio.hpp"
Asset(a, b, c, d) 
{                       
}:
#include "Portfolio.hpp"
class Portfolio {
public:
    double getPortfolioValue(const int& n) {
        if (position->getAssetBase()->getAssetType() == "EQUITY") {
            return position->getAssetBase()->getMarketValue(const int& n);
        }

        else if (position->getAssetBase()->getAssetType() == "PREFERRED") {
            return position->getAssetBase()->getMarketValue(const int& n);
        }

        else if (position->getAssetBase()->getAssetType() == "BOND"){
            return position->getAssetBase()->getMarketValue(const int& n);
        }
    }
    /* other stuff omitted for brevity */
};
#include "Position.hpp"
#include "Assets.hpp"
class Position;