Ich erhalte den Fehler "Error (active) E0349 no operator "*" matches these operands... operand types are: const Vec2 * float
" in der Funktion Projekt. Ich habe Operator * definiert und es scheint, wie die Parameter passen ... Ich sehe nicht, wo ich falsch gemacht ..Kein Operator "*" stimmt mit diesen Operanden überein ... Operandentypen sind: const Vec2 * float
class Vec2
{
public:
float x;
float y;
Vec2 operator*(const float &right) {
Vec2 result;
result.x = x * right;
result.y = y * right;
return result;
}
float MagnitudeSq() const
{
return sqrt(x * x + y * y);
}
float DistanceSq(const Vec2& v2)
{
return pow((v2.x - x), 2) + pow((v2.y - y), 2);
}
float Dot(const Vec2& v2)
{
return x*v2.x + y*v2.y;
}
Vec2 Project(const Vec2& v2)
{
*this = v2 * std::fmax(0, std::fmin(1, (*this).Dot(v2)/this->MagnitudeSq()));
}
};
Off-Topic: Die Ausführung von 'x * x' ist effizienter als' pow (x, 2) '. –
BTW, Klassenmethoden und Mitglieder benötigen nicht den 'this'-Zeiger. Zum Beispiel können Sie 'Dot' direkt und' MagnitudeSq' aufrufen. –
@ThomasMatthews, Auf der anderen Seite [der Compiler ist ziemlich gut zu optimieren] (https://godbolt.org/g/PBnZaN). – chris