为数组赋值时出现问题。我创建了一个名为 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^^^^^^^^^^^^^^
}
}
我收到错误:
有什么建议吗?
最佳答案
这是因为 yieldCurve[i]
是 Treasury
类型,而 new Treasury(treasuries[i]);
是指向 Treasury
对象的指针。所以你有一个类型不匹配。
尝试更改此行:
yieldCurve[i] = new Treasury(treasuries[i]);
对此:
yieldCurve[i] = Treasury(treasuries[i]);