[英]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.