だから、私は自分の問題を定義するのに最適な仕事をするつもりです。私は基本的な財務の割り当てに取り組んでいます。異なる基本クラスに複数のヘッダファイルを作成しています。これらはAssets
,Position
、Portfolio
です。 Assets
は私の基本クラスであり、Position
クラスは、前方宣言によってAssets
オブジェクトへの参照を含み、Portfolio
クラスは、前方宣言によってPosition
クラスへの同様の参照を含みます。他の継承されていない複数のクラスを描画するC++クラスのメソッドを実装しますか?
今、私は、そのオブジェクトがEquity
、Preferred
、およびBond
ある資産の派生クラスの一つであるかどうかに基づいて、ポートフォリオのポジション対象の市場価値を返しますgetPortfolioValue
と呼ばれるポートフォリオにメソッドを作成しようとしています。
これらのそれぞれは、その継承したゲッタ(getAssetType
)がその資産タイプがどのようなものであるかによって、(実際に数値回答を生成する)メソッドgetMarketValue
を持っています。
getPortfolioValueはPortfolio.hpp
で定義され、Position.hpp
(forward-declarationのため)で実装されていますが、すべての種類の「不完全型」エラーが発生します。問題のある部分は、最後から2番目のコード(Position.hpp
)の一番下にあります。
私はできるだけクリーンなMWEを提供しようとしました。 >getPortfolioValue
メソッドを2つ以上のヘッダファイルに分割しなければならないようにするにはどうすればいいですか?非常に事前にありがとうございます。エラーの
ここでは私のコードは次のとおりです。
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";
}
Position.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 */
Portfolio.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 */
なぜこれらのif文を持っていますか?継承のポイントはそれを避けることです。 –
if文が実際には不要な場合もありますが、それ以外はどうすればよいでしょうか。たとえ私が何らかの形でifステートメントを取り除いたとしても、return-> ... codeビットは同じエラーを返します。 – Coolio2654
あなたの外見は変です。 "Assets.hpp"は "Position.hpp"と "Portfolio.hpp"に依存しないので、これらは削除する必要があります。また、 'Position'クラスは' Portfolio'に依存しないので、 'Position.hpp 'からインクルードを削除する必要があります。 'Portfolio :: getPortfolioValue()'の定義をそれが属する「Portfolio.hpp」に入れます。 "Portfolio.hpp"には "Position.hpp"のみが含まれています。 – zett42