C++ 乘法运算符不返回值
我有一个Matrix3类,它有一个重载的*运算符,所以我应该能够:C++ 乘法运算符不返回值,c++,visual-c++,C++,Visual C++,我有一个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计算的结果。相反,它似乎仍然被分配了创建时内存中的任何内容 我在调试器中查看了它,运算符*方法末尾的结果代码被分配到正确的值,但由于某种原因,
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计算的结果。相反,它似乎仍然被分配了创建时内存中的任何内容
我在调试器中查看了它,运算符*方法末尾的结果代码被分配到正确的值,但由于某种原因,它从未被分配
我正在运行Visual Studio 2017(我尝试更新到最新版本),目前正在使用调试x86配置文件运行
我还应该指出,我没有使用预先制作的数学库,因为这是作业的一部分
以下是该类的完整代码:
matrix3.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);
};
#pragma一次
#包括“vector3.h”
类Matrix3
{
公众:
联盟
{
结构
{
矢量3轴;
矢量3轴;
联合{
向量3;
向量3翻译;
};
};
矢量3轴[3];
浮动数据[3][3];
};
静态常数矩阵3恒等式;
矩阵3(向量3x,向量3y,向量3z):xAxis{x},yAxis{y},zAxis{z}
矩阵3(浮点f1,浮点f2,浮点f3,浮点f4,浮点f5,浮点f6,浮点f7,浮点f8,浮点f9):数据{f1,f2,f3,f4,f5,f6,f7,f8,f9}{}
Matrix3():Matrix3{identity}{
矩阵3(常数矩阵3和其他)
{
对于(int i=0;i>3;i++)
轴[i]=其他轴[i];
}
矩阵3和运算符=(常数矩阵3和其他);
向量3和运算符[](整数索引);
常量向量3和运算符[](整数索引)常量;
Matrix3运算符*(常数Matrix3和其他)常数;
向量3运算符*(常量向量3&v)常量;
Matrix3运算符+(常量Matrix3和其他)常量;
Matrix3运算符-(常量Matrix3和其他)常量;
运算符float*(){return reinterpret_cast(data);}
矩阵3转置()常数;
无效设置刻度(浮动x、浮动y、浮动z);
无效设置比例(常数向量3&v);
空隙比例(浮动x、浮动y、浮动z);
空隙率(常数矢量3 v);
void setRotateX(浮动弧度);
void setRotateY(浮动弧度);
void setRotateZ(浮动弧度);
void rotateX(浮动弧度);
空隙旋转(浮动弧度);
void rotateZ(浮动弧度);
void setEuler(浮动俯仰、浮动偏航、浮动滚转);
运算符std::string()常量;
朋友STD::OsFase&运算符< P>代码未显示行为。在C++中,只有一个成员的<代码>联合< /COD>任何时候都是活动的。只有<代码>联盟< /COD>的活动成员可以被读取。分配给<代码>联盟< /C> >成员使其活动,并使所有其他成员不活动。例如,如果您将值赋给identity.data
尝试从identity.axis
读取数据是未定义的行为,直到您为identity.axis
指定了一个值,之后您就不能再从identity.data
读取数据。这与c中的工作方式不同
通过调用void Matrix3::setRotateX(浮动弧度)
将值分配给联合
的xAxis
、yAxis
和zAxis
成员,使联合的结构
组件处于活动状态。然后,当将m2
与m3
相乘时,调用Matrix3 Matrix3::operator*(const Matrix3&other)const
读取未激活的联合
的数据
成员,从而导致未定义的行为
最简单的解决方案可能是将<代码>联合<代码>一起使用,只使用一个表示。< /P> < P>代码显示了未定义的行为。在C++中,只有一个成员的<代码>联合< /COD>任何时候都是活动的。只有<代码>联盟< /代码>的活动成员可以被读取。分配给<代码>联合。member使其处于活动状态,并使所有其他成员处于非活动状态。例如,如果您为
identity.data
分配了一个值,则尝试从identity.axis
读取数据是未定义的行为,直到您为identity.axis
分配了一个值,之后您就不能再从identity.data
读取数据。这是不同的om它在c中是如何工作的
通过调用void Matrix3::setRotateX(浮动弧度)将值分配给联合
的xAxis
、yAxis
和zAxis
成员,使联合的结构
组件处于活动状态。然后,当将m2
与m3
相乘时,调用Matrix3 Matrix3::operator*(const Matrix3&other)const
读取未激活的联合
的数据
成员,从而导致未定义的行为
最简单的解决方案可能是一起处理
联合
,只使用其中一种表示法。你不能使用联合
来映射那样的数据。使用任何不是最后一个被分配到的联合
成员都是未定义的行为。我不知道你的意思。是因为我是我们吗浮动和矢量3对象,还是在联合内部使用联合?这解决了问题,非常感谢,在过去的几个小时里,我一直在用头撞墙:)。你应该把它作为一个答案发布,这样我就可以给你评分了。这是很多代码。请尽量精简(不牺牲正确性或再现性)。你不能使用union
来映射那样的数据。使用任何不是最后一个被分配到的union
成员都是未定义的行为。我不确定我是否理解你的意思。是因为我同时使用float和Vector3对象,还是因为在union中使用了union?这解决了问题非常感谢你,一直在抨击m在最后的几个小时里,我一直靠在墙上:)。你应该把它作为一个答案贴出来,这样我才能给你信用。这是一个错误
#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;
}