• Register
1,240 points
10 7 4

We often get errors in programming. Let me break down the error “not match for operator ‘=’” the error is trying to tell us that the operands we used around the operator are miss matched. Yes! I know it seems to be bullshit but let’s try to understand it by an example.

 

Example

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^^^^^^^^^^^^^^
    }
}

You can check out whole code snippet provided by checking this link.

Cause

Here yieldCurve[i] is of type Treasury and new Treasury(treasuries[i]); is pointer to treasury. So it is a mismatch. As I told you before this error occurs due to the mismatch of the data type of the operands around the operator.

Note: Mostly, the data type of the operands will be user defined data types.

It won’t be the same case if use ‘=’ operator for two ordinary data types. The compiler will compile the code, because here the value on the right hand side is not a r-value since for cases like a= b; it is not a proper assignment operator. It is just swapping the address.

 

Solution

Till now you should have an idea, what is the reason behind the error so can check out your types of the operands. However for the example given above the solution is-

 

Try changing this line:

 

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

to this:

 

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