C++ 错误:“operator=”不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7788612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Error: No match for 'operator='
提问by CHawk
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:
为数组赋值时出现问题。我创建了一个名为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);
};
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]);'
不匹配
'operator='
就行'yieldCurve[i] = new Treasury(treasuries[i]);'
Any advice?
有什么建议吗?
回答by Mysticial
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]
is 的类型Treasury
,并且new Treasury(treasuries[i]);
是指向Treasury
对象的指针。所以你有一个类型不匹配。
Try changing this line:
尝试更改此行:
yieldCurve[i] = new Treasury(treasuries[i]);
to this:
对此:
yieldCurve[i] = Treasury(treasuries[i]);
回答by K-ballo
Treasury* yieldCurve;
yieldCurve
is a pointer to an array of Treasury
, not Treasury*
. Drop the new
at the line with error, or modify the declaration for it to be an array of pointers.
yieldCurve
是一个指向 的数组的指针Treasury
,而不是Treasury*
。删除new
有错误的行,或将声明修改为指针数组。