gpt4 book ai didi

c++ - 乘法运算符不返回值

转载 作者:行者123 更新时间:2023-11-28 01:35:10 26 4
gpt4 key购买 nike

我有一个 Matrix3 类,它有一个重载的 * 运算符,所以我应该可以去:

Matrix3 m1, m2, m3
m2.setRotateX(3.98f); //values are not important just setting a value.
m3.setRotateZ(9.62f);

m1 = m2 * m3

预期结果:m1 被赋予 m2 * m3 计算的结果。相反,它似乎仍被分配了创建时内存中的任何内容。

我在调试器中查看了它,operator * 方法末尾的结果代码被分配给了正确的值,但由于某种原因它从未被分配。

我正在运行 Visual Studio 2017(我尝试更新到最新版本)并且当前使用 Debug x86 配置文件运行。

我还应该指出,我没有使用预制的数学库,因为这是作业的一部分。

这是该类的完整代码:矩阵3.h

#pragma once
#include "vector3.h"

class Matrix3
{
public:
union
{
struct
{
Vector3 xAxis;
Vector3 yAxis;
union {

Vector3 zAxis;
Vector3 translation;
};
};
Vector3 axis[3];
float data[3][3];
};
static const Matrix3 identity;

Matrix3(Vector3 x, Vector3 y, Vector3 z) : xAxis{ x }, yAxis{ y }, zAxis{ z } { }
Matrix3(float f1, float f2, float f3, float f4, float f5, float f6, float f7, float f8, float f9) : data{ f1, f2, f3, f4, f5, f6, f7, f8, f9 } {}
Matrix3() : Matrix3{ identity } {}

Matrix3(const Matrix3 &other)
{
for (int i = 0; i > 3; i++)
axis[i] = other.axis[i];
}




Matrix3& operator= (const Matrix3& other);

Vector3& operator[] (int index);
const Vector3& operator[] (int index) const;
Matrix3 operator * (const Matrix3& other) const;
Vector3 operator * (const Vector3& v) const;

Matrix3 operator + (const Matrix3& other) const;
Matrix3 operator - (const Matrix3& other) const;
operator float*() { return reinterpret_cast<float*>(data); }

Matrix3 transpose() const;

void setScaled(float x, float y, float z);
void setScaled(const Vector3& v);
void scale(float x, float y, float z);
void scale(const Vector3 v);

void setRotateX(float radians);
void setRotateY(float radians);
void setRotateZ(float radians);

void rotateX(float radians);
void rotateY(float radians);
void rotateZ(float radians);

void setEuler(float pitch, float yaw, float roll);


operator std::string() const;
friend std::ostream& operator<< (std::ostream& os, const Matrix3& matrix);
};

矩阵3.cpp

#include "Matrix3.h"

const Matrix3 Matrix3::identity = Matrix3({ 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 });
static const int MATRIX_DIMS = 3;


Vector3& Matrix3::operator[] (int index)
{
return axis[index];
}

const Vector3& Matrix3::operator[] (int index) const
{
return axis[index];
}


Matrix3& Matrix3::operator=(const Matrix3& other)
{
xAxis = other.xAxis;
yAxis = other.yAxis;
zAxis = other.zAxis;

return *this;
}

Matrix3 Matrix3::operator * (const Matrix3& other) const
{
Matrix3 result;

for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
result.data[c][r] = data[0][r] * other.data[c][0] +
data[1][r] * other.data[c][1] +
data[2][r] * other.data[c][2];

return result;
}

Vector3 Matrix3::operator * (const Vector3& v) const
{
Vector3 result;

for (int r = 0; r < 3; ++r)
{
result[r] = 0;
for (int i = 0; i < MATRIX_DIMS; ++i)
result[r] += data[i][r] * v[i];
}

return result;
}

Matrix3 Matrix3::operator+(const Matrix3& other) const
{
Matrix3 result;

for (int x = 0; x < MATRIX_DIMS; x++)
for (int y = 0; y < MATRIX_DIMS; y++)
result[x][y] = data[x][y] + other[x][y];

return result;
}

Matrix3 Matrix3::operator-(const Matrix3& other) const
{
Matrix3 result;

for (int x = 0; x < MATRIX_DIMS; x++)
for (int y = 0; y < MATRIX_DIMS; y++)
result[x][y] = data[x][y] - other[x][y];

return result;
}

Matrix3 Matrix3::transpose() const
{
Matrix3 result;

for (int r = 0; r < MATRIX_DIMS; ++r)
for (int c = 0; c < MATRIX_DIMS; ++c)
result.data[r][c] = data[c][r];

return result;
}

void Matrix3::setScaled(const Vector3& v)
{
// set scale of each axis
xAxis = { v.x, 0, 0 };
yAxis = { 0, v.y, 0 };
zAxis = { 0, 0, v.z };
}

void Matrix3::setScaled(float x, float y, float z)
{
// set scale of each axis
xAxis = { x, 0, 0 };
yAxis = { 0, y, 0 };
zAxis = { 0, 0, z };
}

void Matrix3::scale(const Vector3 v)
{
Matrix3 m;
m.setScaled(v.x, v.y, v.z);

*this = *this * m;
}

void Matrix3::scale(float x, float y, float z)
{
Matrix3 m;
m.setScaled(x, y, z);

*this = *this * m;
}

void Matrix3::rotateX(float radians)
{
Matrix3 m;
m.setRotateX(radians);

*this = *this * m;
}

void Matrix3::rotateY(float radians)
{
Matrix3 m;
m.setRotateY(radians);

*this = *this * m;
}

void Matrix3::rotateZ(float radians)
{
Matrix3 m;
m.setRotateZ(radians);

*this = *this * m;
}

void Matrix3::setRotateX(float radians)
{
xAxis = { 1, 0, 0 };
yAxis = { 0, cosf(radians), sinf(radians) };
zAxis = { 0, -sinf(radians), cosf(radians) };
}

void Matrix3::setRotateY(float radians)
{
xAxis = { cosf(radians), 0, -sinf(radians) };
yAxis = { 0, 1, 0 };
zAxis = { sinf(radians), 0, cosf(radians) };
}

void Matrix3::setRotateZ(float radians)
{
xAxis = { cosf(radians), sinf(radians), 0 };
yAxis = { -sinf(radians), cosf(radians), 0 };
zAxis = { 0, 0, 1 };
}

void Matrix3::setEuler(float pitch, float yaw, float roll)
{
Matrix3 x, y, z;
x.setRotateX(pitch);
y.setRotateY(yaw);
z.setRotateZ(roll);

*this = z * y * x;
}


Matrix3::operator std::string() const
{
std::string result = "";

for (int r = 0; r < MATRIX_DIMS; ++r)
{
result += "{";
for (int c = 0; c < MATRIX_DIMS; ++c)
{
result += std::to_string(data[r][c]);

if (r != MATRIX_DIMS - 1 || c != MATRIX_DIMS - 1)
result += ",";
}
result += "}";
}

return result;

}

std::ostream& operator<< (std::ostream& os, const Matrix3& matrix)
{
os << static_cast<std::string>(matrix);
return os;
}

最佳答案

提供的代码表现出未定义的行为。在 C++ 中,union 中任何时候最多只有一个成员处于事件状态。只能读取 union 的事件成员。分配给一个 union 成员会使它活跃并使所有其他成员不活跃。例如,如果您为 identity.data 赋值,则尝试从 identity.axis 中读取是未定义的行为,直到您为 identity.axis< 赋值,之后您将无法再读取 identity.data。这与它在 c 中的工作方式不同。

通过调用 void Matrix3::setRotateX(float radians),您可以将值分配给 unionxAxisyAxiszAxis 成员,使 unionstruct 组件处于事件状态。然后,当您将 m2m3 相乘时,您调用 Matrix3 Matrix3::operator * (const Matrix3& other) const 读取uniondata 成员未激活,这会导致未定义的行为。

最简单的解决方案可能是一起处理 union 并仅使用其中一种表示形式。

关于c++ - 乘法运算符不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com