我有一个问题搞清楚使用[]操作符来指定值,我有2个不同的错误,重载托架操作者分配
“二进制‘=’:没有操作员发现它接受一个右边的操作数类型'双'(或没有可接受的转换)“
没有运算符”=“匹配这些操作数。
我在这里错过了什么吗?
页眉
Class CMyVector{
public:
double operator[](const int index) const;
double & operator[](const int index);
CMyVector operator+(const CMyVector mv1);
}
CPP
double CMyVector::operator[](const int index) const
{
return arr[index];
}
double & CMyVector::operator[](const int index)
{
return arr[index];
}
CMyVector CMyVector::operator+(const CMyVector mv1)
{
CMyVector *retval = new CMyVector();
retval[0] = arr[i] + mv1[i];
return *this;
}
工作就像一个魅力,但我只是用*这在下面的答案中提到。 –
@JoachimGotzes这有一个完全不同的含义,不会给你你想要的。请注意,Joachim从他的回答中删除了这一点,他可能认为你正在执行'operator + ='而不是其他东西。 – TartanLlama
是的,你是正确的,但我仍然工作,如果我说vec = vec2 * 2;我有vec的正确值。 –