我正在研究Vector2D類,我認爲使用+ =/+運算符可以實現矢量加法和標量加法。如何使用+ =運算符實現標量和矢量加法?
麻煩的是,我真的不知道如何解決這個明顯的說法含糊不清,這裏是鏘這樣說:
vector2d_test.cpp:17:16: error: use of overloaded operator
'+=' is ambiguous (with operand types 'Vector2D<float>' and 'int')
vector += 1;
~~~~~~^~~~~~~~
vector2d.hpp:34:18: note: candidate function
Vector2D<T>& operator+=(const Vector2D<T>& other)
^
vector2d.hpp:41:18: note: candidate function
Vector2D<T>& operator+=(const T summand) const
這裏有兩個功能:
Vector2D<T>& operator+=(const Vector2D<T>& other)
{
x += other.x;
y += other.y;
return *this;
}
template <typename S>
Vector2D<T>& operator+=(const S summand) const
{
x += summand;
y += summand;
return *this;
}
所以..任何想法我能做些什麼?
模糊性必須來自'Vector2D'的構造函數'帶'int'。人們也觀察到第二個成員錯誤的「const」,導致我們得出結論,這絕對不是足夠的代碼。 –
Puppy
2013-03-15 09:59:12
@DeadMG虛假的'const'顯然是錯誤的,我忽略了它。 'operator + ='應該_never_是'const'。但是,正如我在編輯中指出的那樣,您仍然需要隱式轉換才能使調用不明確。 – 2013-03-15 10:05:15
哎呀,這個常量實際上是問題:P – futlib 2013-03-15 12:03:19