C++ - 何时准确使用->?错误:‘->’ 的基操作数具有非指针类型“BaseBond”;
遇到一些非常令人困惑的错误,并且不确定到底为什么。这是我的代码:
//=====================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 是一个公共类变量。为什么它会给我一个底部方法的错误,而不是构造函数的错误?我不明白。
我还尝试在第二种方法中将所有 tradeBook[i]->method() 更改为 TradingBook[i].method() ,但它给了我一个分段错误,所以我认为这是不对的。谁能帮我解答我的困惑吗?
如果需要的话,这里是 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 的标题:
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!
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());
}
}
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.
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?
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 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;
};
Thank you in advance, stackOverFlow!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为
tradingBook[i]
是一个对象,而不是指针,因为您引用的是成员变量tradingBook
,它被定义为TradingBook *tradingBook
。因此,请使用tradingBook[i].
而不是tradingBook[i]->
。That's because
tradingBook[i]
is an object, not a pointer, since you are referring to the member variabletradingBook
which is defined asTradingBook *tradingBook
. So, usetradingBook[i].
instead oftradingBook[i]->
.这是因为:
在构造函数中构造了一个隐藏
tradingBook
的作用域变量。将整行更改为:行
并将标题中的
更改为,它应该可以工作。
It because this:
in the constructor constructs a scoped variable that hides
tradingBook
. change that whole line to:and the line
in the header to
and it should work.
Dani 是正确的,您应该使用以下方式声明和分配基类型的数组:(
但如果它是可变大小的并且您要追加和删除,则需要
malloc()
(痛苦)或者使用 STL Vector(/无论什么)而不是数组。为了简单起见,分配您可能合理需要的最大数组。)无论如何,听起来编译器(和文档)告诉您“BaseBond”是一个抽象基类,不能可以直接实例化(但它会有可以的子类)。通过阅读文档或代码示例来找出哪些子类(如果您在阅读后无法弄清楚,则将其链接到此处)。
或者您应该定义自己的子类
class MyInheritedBond (BaseBond)
并实现任何纯虚拟方法(即编译器喋喋不休的任何方法):Dani is correct that you should declare and allocate an array of your base-type with:
(But if it's variable-sized and you will be appending and removing, you'll either need
malloc()
(painful) or else use an STL Vector(/whatever) instead of array. For simplicity, allocate the largest array you might reasonably need.)Anyway, sounds like the compiler (and documentation) tell you 'BaseBond' is an abstract base class which cannot be directly instantiated (but it will have subclasses which can). Figure out which subclasses by reading the documentation or code examples (or link it here if you can't figure that out after reading).
Either that or you're supposed to define your own subclass
class MyInheritedBond (BaseBond)
and implement whatever methods are pure virtual (i.e. whichever methods the compiler nags about):