c++ - 实现利用多个其他非继承类的 C++ 类方法?

标签 c++ class c++11 inner-classes forward-declaration

因此,我将尽最大努力定义我的问题,所以请多多包涵。我正在做一项基本的财务任务,其中我为不同的基类制作了多个头文件:它们是 AssetsPositionPortfolioAssets 是我的基类,Position 类通过前向声明包含对 Assets 对象的引用,而 Portfolio 类通过前向声明包含对 Position 类的类似引用。

现在,我试图在 Portfolio 中创建一个名为 getPortfolioValue 的方法,该方法将根据该对象是否是 Asset 的派生类之一返回投资组合的 Position 对象的市场值(value),这些派生类是 股权优先股债券

它们中的每一个都有一个 getMarketValue 方法,根据它们继承的 getter getAssetType 告诉我它们的 Assets 类型是什么,该方法被调用(实际上生成数字答案) .

getPortfolioValue 在 Portfolio.hpp 中定义,并在 Position.hpp 中实现(由于前向声明),但给出各种“不完整类型”错误。有问题的部分在倒数第二个代码 (Position.hpp) 的底部。

我已尝试提供尽可能干净的 MWE。有人知道如何使这个必须跨越 >2 个头文件的 getPortfolioValue 方法起作用吗?非常感谢您。

错误图片: Picture of error

这是我的代码:

Assets .hpp

#ifndef Assets_hpp
#define Assets_hpp
#include <stdio.h>
#include <fstream>
#include <string>
#include <cmath>

#include "Position.hpp"
#include "Portfolio.hpp"

using namespace std;


class Asset{
private:
    string assetType;
    double currentPrice;
    string securityIssuer;
    string securitySymbol;

public:
    Asset(const string& a = "n/a", const double& b = 0.0, const string& c = "n/a", const string& d = "n/a") :
    assetType(a), currentPrice(b), securityIssuer(c), securitySymbol(d)
    {
    };

    virtual string getAssetType();
    virtual double getMarketValue(const int& n);

    virtual ~Asset() {};

};

class Equity: public Asset{
public:

    Equity(const string& a = "EQUITY", const double& b = 0.0, const string& c = "n/a", const string& d = "n/a") :
    Asset(a, b, c, d) 
    {                       
    }:

    virtual string getAssetType(); //Virtual version of Asset's getAssetType
    virtual double getMarketValue(const int& n);


};

class Preferred: public Equity {
public:
    Preferred(const string& a = "PFD", const double& b = 0.0, const string& c = "n/a", const string& d = "n/a") :
    Equity(a, b, c, d)
    {
    };

    virtual double getMarketValue(const int& n);
};


class bond: public Asset{
private:
    double coupon_rate;
    double coupon_freq;
    double par;

public:
    bond(const string& a = "BOND", const double& b = 0.0, const string& c = "n/a", const string& d = "n/a", double e = 0.0, double f = 0.0, double g = 0.0) :
    Asset(a, b, c, d), coupon_rate(e), coupon_freq(f), par(g)
    {
    };

    double bond_value(int num_bonds);

    virtual string getAssetType(); //Virtual version of Asset's getAssetType
    virtual double getMarketValue(const int& n);

};

#endif /* Assets_hpp */

Assets .cpp

#include "Assets.hpp"

string Asset::getAssetType() {
    return assetType;
}


string Equity::getAssetType() { //Virtual implementation for Equity.
    return "EQUITY";
}


string bond::getAssetType() { //Virtual implementation for bond.
    return "BOND";
}

位置.hpp

#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 */

投资组合.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 */

最佳答案

您的包含不是基于类的依赖性,这是主要问题的来源。

"Assets.hpp" 中删除这些行,因为类 AssetEquityPreferredbond 不依赖于 PositionPortfolio:

#include "Position.hpp"
#include "Portfolio.hpp"

这里有一个语法错误(去掉“:”和同样函数后的过时的“;”):

Asset(a, b, c, d) 
{                       
}:

"Position.hpp" 中删除这一行,因为 Position 不依赖于 Portfolio:

#include "Portfolio.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 类依赖于 PositionAsset,所以您必须在 "Portfolio.hpp":

#include "Position.hpp"
#include "Assets.hpp"

同时从 "Portfolio.hpp" 中删除前向声明:

class Position;

当您调用Position 的方法时,前向声明是没有用的。您需要完整的声明。

这应该可以修复编译错误。

为什么在 getPortfolioValue() 中有这些 if 仍然很奇怪。您在所有这些分支中都在做同样的事情...

关于c++ - 实现利用多个其他非继承类的 C++ 类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47623659/

相关文章:

c++ - 什么时候应该添加单词 "class"在 C++ 中创建指针?

c++ - 创建在另一个项目中定义的类对象

c++ - 创建字符串数组的有效方法

c++ - "Inheriting"具有可变模板函数的类

c++ - 如何使用 'getaddrinfo'为所有接口(interface)选择默认空闲端口?

android - 如何将 "Chrome"(或等效项)作为线程而不是进程启动?

jQuery 切换类

c++ - 将可变参数函数参数转发到另一个可变参数函数而无需成本

c++ - MFC静态库和外部rc(resource)文件图标加载问题

<< 运算符的 c++11 特定重载