7

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?

iammilind
  • 68,093
  • 33
  • 169
  • 336
CHawk
  • 1,346
  • 5
  • 22
  • 40
  • 5
    This is the exact same problem you [asked about yesterday](http://stackoverflow.com/questions/7781292/c-when-to-exactly-use-error-base-operand-of-has-non-pointer-type). The only difference is that it's happening with a different variable. Perhaps it's time to invest in a good C++ book, as your understanding of arrays and pointers could use improvement. – Nicol Bolas Oct 17 '11 at 02:58

2 Answers2

13

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]);
Mysticial
  • 464,885
  • 45
  • 335
  • 332
4
    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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169