为数组赋值时出现问题。我创建了一个名为 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^^^^^^^^^^^^^^
}
}
我收到错误:
No match for
'operator='
on the line'yieldCurve[i] = new Treasury(treasuries[i]);'
有什么建议吗?
最佳答案
那是因为 yieldCurve[i]
是 Treasury
类型,而 new Treasuries(treasuries[i]);
是一个指向金库
对象。所以你的类型不匹配。
尝试改变这一行:
yieldCurve[i] = new Treasury(treasuries[i]);
为此:
yieldCurve[i] = Treasury(treasuries[i]);
关于C++ 错误 : No match for 'operator=' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788612/