问题描述
将值分配给数组时出现问题。我创建了一个名为库房
的课程。我创建了另一个名为 TradingBook
的类,该类包含一个 Treasury
的全局数组,可以从其中的所有方法访问该数组。 TradingBook
。这是我的TradingBook和Treasury头文件:
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^^^^^^^^^^^^^^
}
}
我遇到了错误:
有任何建议吗?
推荐答案
这是因为 yieldCurve [i]
的类型为库房
和 new Treasury(treasuries [i]);
是指向 Treasury
对象的指针。因此,您的类型不匹配。
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.
尝试更改此行:
yieldCurve[i] = new Treasury(treasuries[i]);
为此:
yieldCurve[i] = Treasury(treasuries[i]);
这篇关于C ++错误:“ operator =”不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!