问题描述
得到一些真正令人困惑的错误,不确定确切原因.这是我的代码:
Getting some really confusing errors and not sure exactly why. Here is my code:
//=====================WORKS=======================
TradingBook::TradingBook(const char* yieldCurvePath, const char* bondPath)
{
//...Some stuff
BaseBond* tradingBook[bondCount];
for (int i=0; i < bondCount; i++)
{
tradingBook[i] = new CouponBond(bonds[i]);
printf("Bond: %s\n"
" Price: %.3f\n"
" DV01: %.3f\n"
" Risk: %.3f\n", tradingBook[i]->getID(), tradingBook[i]->getPrice(), tradingBook[i]->getDV01(), tradingBook[i]->getRisk());
}
}
//=================DOESNT WORK======================
//Gives Error: base operand of ‘->’ has non-pointer type ‘BaseBond’
void TradingBook::runAnalytics()
{
for (int i=0; i < bondCount; i++)
{
tradingBook[i]->calcPrice(tradingBook[i]->getYield());
tradingBook[i]->calcDV01();
tradingBook[i]->calcRisk();
printf("Bond: %s\n"
" Price: %.3f\n"
" DV01: %.3f\n"
" Risk: %.3f\n", tradingBook[i]->getID(), tradingBook[i]->getPrice(), tradingBook[i]->getDV01(), tradingBook[i]->getRisk());
}
}
我得到的错误基本操作数->"在->的每个实例中都具有非指针类型"BaseBond",但仅在runAnalytics()方法中. tradingBook是一个公共类变量.为什么它会在bottom方法中给我一个错误,但在构造函数中却给我一个错误呢?我不明白.
I get the error base operand of ‘->’ has non-pointer type ‘BaseBond’ at every instance of ->, but only in the runAnalytics() method. tradingBook is a public class variable. Why would it give me an error in the bottom method but not the constructor? I don't get it.
我还尝试在第二种方法中将所有tradingBook [i]-> method()更改为tradingBook [i] .method(),但这给我带来了细分错误,因此我认为这是不对的.谁能帮助我消除混乱?
I also tried changing all the tradingBook[i]->method() to tradingBook[i].method() in the 2nd method, but it gives me a segmentation fault so I figured that isn't right. Can anyone help me clear up my confusion?
如果需要,这是TradingBook的头文件:
In case it is needed, here is the header file for TradingBook:
class TradingBook
{
public:
TradingBook(const char* yieldCurvePath, const char* bondPath);
double getBenchmarkYield(short bPeriods) const;
BaseBond* tradingBook;
int treasuryCount;
Treasury* yieldCurve;
int bondCount;
void runAnalytics();
};
BaseBond是一个抽象类.它的孩子是CouponBond和ZeroCouponBond. tradingBook [i]都同时填充了这两种类型.这是BaseBond的标题:
BaseBond is an abstract class. It's children are CouponBond and ZeroCouponBond. tradingBook[i] is filled with both types. Here is the header for BaseBond:
class BaseBond{
public:
virtual double getPrice() = 0;
virtual double getYield() = 0;
virtual short getPeriods() = 0;
virtual char* getID() = 0;
virtual double getDV01() = 0;
virtual double getRisk() = 0;
virtual char* getRateType() = 0;
virtual void calcPrice(double nyield) = 0;
virtual double calcPriceR(double nyield) const = 0;
virtual void calcDV01() = 0;
virtual void calcRisk() = 0;
};
预先感谢您,stackOverFlow!
Thank you in advance, stackOverFlow!
推荐答案
原因如下:
BaseBond* tradingBook[bondCount];
构造函数中的
构造一个隐藏tradingBook
的作用域变量.将整个行更改为:
in the constructor constructs a scoped variable that hides tradingBook
. change that whole line to:
tradingBook = new BaseBond*[bondCount];
和行
BaseBond* tradingBook;
在标题中
BaseBond** tradingBook;
它应该可以工作.
这篇关于C ++-何时确切使用-> ;?错误:“->"的基本操作数具有非指针类型"BaseBond"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!