我有一個問題搞清楚使用[]操作符來指定值,我有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的正確值。 –