C++ 错误 : No match for 'operator='

标签 c++ compiler-errors

为数组赋值时出现问题。我创建了一个名为 Treasury 的类。我创建了另一个名为 TradingBook 的类,我希望它包含一个全局数组 Treasury,可以从 TradingBook 中的所有方法访问它。这是我的 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^^^^^^^^^^^^^^
    }
}

我收到错误:

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

有什么建议吗?

最佳答案

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

尝试改变这一行:

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

为此:

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

关于C++ 错误 : No match for 'operator=' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788612/

相关文章:

c++ - 涉及 `crti.o` 和 `crt1.o` 的奇怪链接器错误

C:数组错误初始化的奇怪行为

Java问题;无法将 Activity 添加到 ArrayList<???> 需要帮助

c++ - 为什么 C++ 编译器会在 "c:\program files\gnu emacs\include"中查找包含文件?

c++ - 接受字符串和整数的可变参数函数,格式化后者并连接所有?

c++ - 为什么我得到 "Candidate constructor not viable"?

c++ - 如何使用取决于模板参数的字符类型定义字符串文字?

compiler-errors - unique_ptr 作为类成员和移动语义无法使用 clang 编译

c++ - 如何构建 std::vector<std::string> 然后对它们进行排序?

c++ - 为什么 boost circular_buffer 不存储我的 bool 值?