【发布时间】:2026-01-16 08:55:02
【问题描述】:
所以我在实现自己的四元数类时遇到了一个从成员函数调用我的一个运算符(静态)函数时没有预料到的问题。见函数rotateByVector();
class Quaternion
{
public:
double x, y, z, w;
Quaternion(double w, double xi, double yj, double zk)
{
w = w;
x = xi;
y = yj;
z = zk;
}
Quaternion operator +(Quaternion q1, Quaternion q2)
{
return Quaternion(q1.w + q2.w, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z);
}
Quaternion operator*(Quaternion q1, Quaternion q2)
{
return Quaternion(q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
, q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y
, q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z
, q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x);
}
void rotateByVector(double x1, double y1, double z1, double[3] res)
{
// want to calculate Hamilton Product
// res_quaternion = this_quaternion * quaternion_vector(0, x1, y1, z1) * this_conj;
// return double[3] {res_quaternion.x, res_quaternion.y, res_quaternion.z}
Quaternion q = Quaternion(0.0f, x1, y1, z1);
Quaternion r = (*this).operator*() q; //doesn't like it
Quaternion r = this->operator*(q); //doesn't like it either
...
r = r * this.conj();
res[0] = r.x;
res[1] = r.y;
res[2] = r.z;
}
}
我应该如何实现this quaternion乘以参数的一乘以this quaternion的共轭的乘法
我知道我很了解,但我肯定错过了一些东西。
【问题讨论】:
-
this.conj();不是一个东西。operator*()在哪里?请贴出真实代码。 -
(*this).operator*() q你想在这里使用运算符重载将你的 2 个类对象相乘吗?? -
您显示的是
+运算符,但您使用的是*运算符。请edit您的问题与minimal reproducible example 或SSCCE (Short, Self Contained, Correct Example) -
另外,您正在返回一个指向局部变量的指针:
return double[3] { r.X, r.Y, r.Z };。 -
而且您还声明了一个静态运算符重载,这是非法的。该代码有十种无法编译的方式。
标签: c++