C++错误:与 'operator=' 不匹配

发布于 2024-12-10 12:47:44 字数 1298 浏览 0 评论 0原文

给数组赋值时遇到问题。我创建了一个名为 Treasury 的类。我创建了另一个名为 TradingBook 的类,我想包含一个可以从 TradingBook 中的所有方法访问的全局 Treasury 数组。这是我的 TradingBook 和 Treasury 的头文件:

class Treasury{
public:
    Treasury(SBB_instrument_fields bond);
    Treasury();
    double yieldRate;
    short periods;
};


class TradingBook
{
public:
    TradingBook(const char* yieldCurvePath, const char* bondPath);
    double getBenchmarkYield(short bPeriods) const;
    void quickSort(int arr[], int left, int right, double index[]);

    BaseBond** tradingBook;
    int treasuryCount;
    Treasury* yieldCurve;
    int bondCount;
    void runAnalytics(int i);
};

这是我收到错误的主要代码:

TradingBook::TradingBook(const char* yieldCurvePath, const char* bondPath)
{
    //Loading Yield Curve
    // ...
    yieldCurve = new Treasury[treasuryCount];

    int periods[treasuryCount];
    double yields[treasuryCount];
    for (int i=0; i < treasuryCount; i++)
    {
        yieldCurve[i] = new Treasury(treasuries[i]);
        //^^^^^^^^^^^^^^^^LINE WITH ERROR^^^^^^^^^^^^^^
    }
}

我收到错误:

'yieldCurve[i] = new Treasury(treasuries[i]);' 行上的 'operator=' 不匹配

有什么建议吗?

Having a problem when assigning a value to an array. I have a class I created called Treasury. I created another class called TradingBook which I want to contain a global array of Treasury that can be accessed from all methods in TradingBook. Here is my header files for TradingBook and Treasury:

class Treasury{
public:
    Treasury(SBB_instrument_fields bond);
    Treasury();
    double yieldRate;
    short periods;
};


class TradingBook
{
public:
    TradingBook(const char* yieldCurvePath, const char* bondPath);
    double getBenchmarkYield(short bPeriods) const;
    void quickSort(int arr[], int left, int right, double index[]);

    BaseBond** tradingBook;
    int treasuryCount;
    Treasury* yieldCurve;
    int bondCount;
    void runAnalytics(int i);
};

And here is my main code where I'm getting the error:

TradingBook::TradingBook(const char* yieldCurvePath, const char* bondPath)
{
    //Loading Yield Curve
    // ...
    yieldCurve = new Treasury[treasuryCount];

    int periods[treasuryCount];
    double yields[treasuryCount];
    for (int i=0; i < treasuryCount; i++)
    {
        yieldCurve[i] = new Treasury(treasuries[i]);
        //^^^^^^^^^^^^^^^^LINE WITH ERROR^^^^^^^^^^^^^^
    }
}

I am getting the error:

No match for 'operator=' on the line 'yieldCurve[i] = new Treasury(treasuries[i]);'

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

路弥 2024-12-17 12:47:44

这是因为 yieldCurve[i] 的类型为 Treasury,而 new Treasury(treasuries[i]); 是一个指向 Treasury 对象。所以你有类型不匹配。

尝试将此行:更改

yieldCurve[i] = new Treasury(treasuries[i]);

为:

yieldCurve[i] = Treasury(treasuries[i]);

That's because yieldCurve[i] is of type Treasury, and new Treasury(treasuries[i]); is a pointer to a Treasury object. So you have a type mismatch.

Try changing this line:

yieldCurve[i] = new Treasury(treasuries[i]);

to this:

yieldCurve[i] = Treasury(treasuries[i]);
柠檬色的秋千 2024-12-17 12:47:44
    Treasury* yieldCurve;

yieldCurve 是指向 Treasury 数组的指针,而不是 Treasury*。将 new 删除到有错误的行,或者将其声明修改为指针数组。

    Treasury* yieldCurve;

yieldCurve is a pointer to an array of Treasury, not Treasury*. Drop the new at the line with error, or modify the declaration for it to be an array of pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文