[英]C++ Error: No match for 'operator='
在為數組賦值時遇到問題。 我有一個名為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^^^^^^^^^^^^^^
}
}
我收到錯誤:
'operator='
在'yieldCurve[i] = new Treasury(treasuries[i]);'
上沒有匹配
有什么建議?
這是因為yieldCurve[i]
屬於Treasury
,而new Treasury(treasuries[i]);
是指向Treasury
對象的指針。 所以你的類型不匹配。
嘗試更改此行:
yieldCurve[i] = new Treasury(treasuries[i]);
對此:
yieldCurve[i] = Treasury(treasuries[i]);
Treasury* yieldCurve;
yieldCurve
是指向一系列Treasury
的指針,而不是Treasury*
。 丟棄new
在與錯誤的行,或修改聲明為它是一個指針數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.