C++错误:'operator='不匹配
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^^^^^^^^^^^^^^
}
}
我得到错误:
'yieldCurve[i] = new Treasury(treasuries[i]);'
线上的'operator='
不匹配
有什么建议吗?
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
,或者修改声明使其成为指针数组。
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- Valarray 切片数组与 'operator*' 不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 使用标准::时间::high_resolution_clock时"operator ="不匹配
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 错误:尝试减去字符串时'operator-'不匹配
- 调用模板方法会导致'operator<'不匹配
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 使用 #omp 时'operator -'不匹配
- 尝试使用 fstream 将字符写入文件:'operator<<'不匹配
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 有没有办法修复此错误:与'operator[]'不匹配(操作数类型为 'empl' 和"int")
- 错误与'operator='不匹配(操作数类型为 'Person' 和"Person*")
- 我得到"将值分配给结构数组 iin 时与'operator='不匹配C++
- 在地图中搜索值时'operator='不匹配
- 错误:'operator<<'和"运算符>>"不匹配。请帮忙?