C++错误:与 'operator=' 不匹配
给数组赋值时遇到问题。我创建了一个名为 Treasury
的类。我创建了另一个名为 TradingBook
的类,我想包含一个可以从 TradingBook
中的所有方法访问的全局 Treasury
数组。这是我的 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] = new Treasury(treasuries[i]);' 行上的
'operator='
不匹配
有什么建议吗?
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^^^^^^^^^^^^^^
}
}
I am getting the error:
No match for
'operator='
on the line'yieldCurve[i] = new Treasury(treasuries[i]);'
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
yieldCurve[i]
的类型为Treasury
,而new Treasury(treasuries[i]);
是一个指向Treasury
对象。所以你有类型不匹配。尝试将此行:更改
为:
That's because
yieldCurve[i]
is of typeTreasury
, andnew Treasury(treasuries[i]);
is a pointer to aTreasury
object. So you have a type mismatch.Try changing this line:
to this:
yieldCurve
是指向Treasury
数组的指针,而不是Treasury*
。将new
删除到有错误的行,或者将其声明修改为指针数组。yieldCurve
is a pointer to an array ofTreasury
, notTreasury*
. Drop thenew
at the line with error, or modify the declaration for it to be an array of pointers.