So, I'm going to do the best job defining my problem, so bear with me please. I am working on a basic finance assignment, wherein I make multiple header files for different base classes: these are Assets
, Position
, and Portfolio
. Assets
is my base class, and the Position
class includes a reference to an Assets
object by forward declaration, and the Portfolio
class contains a similar reference to the Position
class by forward declaration.
Now, I am trying to create a method in Portfolio called getPortfolioValue
that will return the market value of the portfolio's Position object based on whether that object is one of Asset's derived classes, which are Equity
, Preferred
, and Bond
.
Each of these has a getMarketValue
method that is called (that actually generates the numerical answer) depending on what their inherited getter, getAssetType
, tells me their Asset type is.
getPortfolioValue is defined in Portfolio.hpp
, and implemented in Position.hpp
(because of forward-declaration), but gives all sorts of "Incomplete Type" errors. The problematic part is on the bottom of the 2nd-to-last code (Position.hpp
).
I have tried to give the cleanest possible MWE. Would someone have any idea on how to make this getPortfolioValue
method work that has to span >2 header files? Thank you extremely in advance.
Here is my code:
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 */