Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error: No match for '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?

like image 580
CHawk Avatar asked Oct 17 '11 01:10

CHawk


2 Answers

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]);
like image 119
Mysticial Avatar answered Oct 12 '22 22:10

Mysticial


    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.

like image 20
K-ballo Avatar answered Oct 12 '22 22:10

K-ballo