简体   繁体   English

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

[英]C++ Error: No match for 'operator='

Having a problem when assigning a value to an array. 在为数组赋值时遇到问题。 I have a class I created called Treasury . 我有一个名为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 . 我创建了另一个名为TradingBook类,我希望它包含一个全局的Treasury数组,可以从TradingBook所有方法访问。 Here is my header files for TradingBook and 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);
};

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]);' 'operator=''yieldCurve[i] = new Treasury(treasuries[i]);'上没有匹配

Any advice? 有什么建议?

That's because yieldCurve[i] is of type Treasury , and new Treasury(treasuries[i]); 这是因为yieldCurve[i]属于Treasury ,而new Treasury(treasuries[i]); is a pointer to a Treasury object. 是指向Treasury对象的指针。 So you have a type mismatch. 所以你的类型不匹配。

Try changing this line: 尝试更改此行:

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

to this: 对此:

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

yieldCurve is a pointer to an array of Treasury , not Treasury* . yieldCurve是指向一系列Treasury的指针,而不是Treasury* Drop the new at the line with error, or modify the declaration for it to be an array of pointers. 丢弃new在与错误的行,或修改声明为它是一个指针数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM