C++ - 何时准确使用->?错误:‘->’ 的基操作数具有非指针类型“BaseBond”;

发布于 2024-12-10 10:15:26 字数 2412 浏览 1 评论 0原文

遇到一些非常令人困惑的错误,并且不确定到底为什么。这是我的代码:

//=====================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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风夜微凉 2024-12-17 10:15:27

这是因为 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 variable tradingBook which is defined as TradingBook *tradingBook. So, use tradingBook[i]. instead of tradingBook[i]-> .

热情消退 2024-12-17 10:15:27

这是因为:

BaseBond* tradingBook[bondCount];

在构造函数中构造了一个隐藏 tradingBook 的作用域变量。将整行更改为:

tradingBook = new BaseBond*[bondCount];

BaseBond* tradingBook;

并将标题中的

BaseBond** tradingBook;

更改为,它应该可以工作。

It because this:

BaseBond* tradingBook[bondCount];

in the constructor constructs a scoped variable that hides tradingBook. change that whole line to:

tradingBook = new BaseBond*[bondCount];

and the line

BaseBond* tradingBook;

in the header to

BaseBond** tradingBook;

and it should work.

梦中楼上月下 2024-12-17 10:15:27

Dani 是正确的,您应该使用以下方式声明和分配基类型的数组:(

BaseBond** tradingBook;
...
tradingBook = new BaseBond[bondCount];

但如果它是可变大小的并且您要追加和删除,则需要 malloc() (痛苦)或者使用 STL Vector(/无论什么)而不是数组。为了简单起见,分配您可能合理需要的最大数组。)

无论如何,听起来编译器(和文档)告诉您“BaseBond”是一个抽象基类,不能可以直接实例化(但它会有可以的子类)。通过阅读文档或代码示例来找出哪些子类(如果您在阅读后无法弄清楚,则将其链接到此处)。
或者您应该定义自己的子类 class MyInheritedBond (BaseBond) 并实现任何纯虚拟方法(即编译器喋喋不休的任何方法):

class MyInheritedBond (BaseBond) {
    void PVfn1() { /* empty dummy implementation to satisfy the compiler */ }
    int  PVfn2(...) { return NULL; }
    ...
};

Dani is correct that you should declare and allocate an array of your base-type with:

BaseBond** tradingBook;
...
tradingBook = new BaseBond[bondCount];

(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):

class MyInheritedBond (BaseBond) {
    void PVfn1() { /* empty dummy implementation to satisfy the compiler */ }
    int  PVfn2(...) { return NULL; }
    ...
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文