2017-12-03 5 views
0

だから、私は自分の問題を定義するのに最適な仕事をするつもりです。私は基本的な財務の割り当てに取り組んでいます。異なる基本クラスに複数のヘッダファイルを作成しています。これらはAssets,PositionPortfolioです。 Assetsは私の基本クラスであり、Positionクラスは、前方宣言によってAssetsオブジェクトへの参照を含み、Portfolioクラスは、前方宣言によってPositionクラスへの同様の参照を含みます。他の継承されていない複数のクラスを描画するC++クラスのメソッドを実装しますか?

今、私は、そのオブジェクトがEquityPreferred、およびBondある資産の派生クラスの一つであるかどうかに基づいて、ポートフォリオのポジション対象の市場価値を返しますgetPortfolioValueと呼ばれるポートフォリオにメソッドを作成しようとしています。

これらのそれぞれは、その継承したゲッタ(getAssetType)がその資産タイプがどのようなものであるかによって、(実際に数値回答を生成する)メソッドgetMarketValueを持っています。

getPortfolioValueはPortfolio.hppで定義され、Position.hpp(forward-declarationのため)で実装されていますが、すべての種類の「不完全型」エラーが発生します。問題のある部分は、最後から2番目のコード(Position.hpp)の一番下にあります。

私はできるだけクリーンなMWEを提供しようとしました。 >getPortfolioValueメソッドを2つ以上のヘッダファイルに分割しなければならないようにするにはどうすればいいですか?非常に事前にありがとうございます。エラーの

画像: 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"; 
} 

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 */ 
+2

なぜこれらのif文を持っていますか?継承のポイントはそれを避けることです。 –

+0

if文が実際には不要な場合もありますが、それ以外はどうすればよいでしょうか。たとえ私が何らかの形でifステートメントを取り除いたとしても、return-> ... codeビットは同じエラーを返します。 – Coolio2654

+0

あなたの外見は変です。 "Assets.hpp"は "Position.hpp"と "Portfolio.hpp"に依存しないので、これらは削除する必要があります。また、 'Position'クラスは' Portfolio'に依存しないので、 'Position.hpp 'からインクルードを削除する必要があります。 'Portfolio :: getPortfolioValue()'の定義をそれが属する「Portfolio.hpp」に入れます。 "Portfolio.hpp"には "Position.hpp"のみが含まれています。 – zett42

答えて

1

あなたのインクルードは、主な問題の原因となるクラスの依存関係に基づいていません。

投稿者"資産。HPP」クラスAssetEquityPreferredbondPositionPortfolioに依存しないため、これらの行を削除します。

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

ここで構文エラーが(削除あり、 『:』とも時代遅れ 『;』の後似機能):

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

PositionPortfolioに依存しないため、"Position.hpp"からこの行を削除:

#include "Portfolio.hpp" 

はまた、それが属する「Portfolio.hpp」Portfolio::getPortfolioValueの定義を移動します。今すぐあなたのPortfolioクラスがPositionAssetに依存

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"

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

「Portfolio.hpp」から前方宣言を削除します。

class Position; 

あなたがPositionのメソッドを呼び出していると、前方宣言は役に立ちません。あなたは完全な宣言が必要です。

これは、コンパイルエラーを修正するはずです。

ifgetPortfolioValue()にあるのはまだ変わっています。あなたはこれらのすべてのブランチで同じことをしています...

+0

私はあなたのアドバイスを取った(ええ、私のifステートメントも不必要だった)、それを実装しましたが、今私はさらに奇妙なエラーがあります。 https://imgur.com/a/pSvHV これはどういう意味ですか? – Coolio2654

+0

@ Coolio2654あなたはまだ 'getMarketValue()'を実装していません。 – zett42

+0

あなたのアドバイスは、この問題を非常にうまく解決するのに役立ちました。実際には十分ではない場合、フォワード宣言を上回っていることがわかりましたし、回避策を使って作業を進めると、すでにかなり大きなコードにエラーが発生しやすくなりました。私の単純なエラーを指摘してくれてありがとう。 – Coolio2654

0

問題は、あなたがif(position->getAssetBase()->getAssetType() == "Equity")代わりのif(position->getAssetBase()->getAssetType()= "Equity")を行う必要があります。

これが役に立ちます。

+0

こんにちは、私はその素朴なエラーを修正しましたが、キャッチのおかげで、私の根本的な問題はまだそこにあります。 – Coolio2654

+0

私の位置は、ポジションオブジェクトに過ぎません。ポートフォリオは、ある意味では複数のポジションを保持しています。 – Coolio2654

+0

@ coolio2654メインポストに関する私の他のコメントを確認してください –

0

問題を示すインラインメソッドは、asset.hの定義に依存しますが、問題関数を定義する時点ではasset.hは含まれていません。問題関数が問題を修正する前に単にasset.hをインクルードしているかどうかはわかりません(ここには循環依存関係がありません)。

+0

私は両方の後者のヘッダーを私のアセットヘッダーの先頭に入れ、前方に必要なクラスを宣言しました。後者の両方。私はこのメソッドが単純な前方宣言のために働くのは知っていますが、それをもっと複雑に使うのではないようです。 – Coolio2654

関連する問題