将变量移动到 header 文件时出现 运行 时间错误

Getting run time error when moving variable to header file

我正在开发 DirectX 应用程序,我在将 2 个矩阵相乘时遇到了一些问题,当我尝试时出现 运行 时间错误。如果我在我的 header 中声明矩阵,我只会得到错误,如果我在函数中本地声明它们,它工作正常。这不是我在应用程序中遇到此类错误的唯一地方。


GraphicClass.cpp

void GraphicClass::UpdateScene()
{
    Cube1World = DirectX::XMMatrixIdentity();

    DirectX::XMVECTOR rotaxis = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
    Rotation = DirectX::XMMatrixRotationAxis(rotaxis, rot);
    Translation = DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f);

    Cube1World = Translation * Rotation; //I get run time error on this line
}

GraphicClass.h

class GraphicClass{

    DirectX::XMMATRIX Cube1World;
    DirectX::XMMATRIX Rotation;
    DirectX::XMMATRIX Translation;
    float rot = 0.01f;

    //other Code
}

错误信息

Unhandled exception at 0x002F4C0F in Engine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.

我可以在 header 文件中声明 Cube1World,但是如果我尝试使用旋转或平移来进行声明,我会收到 运行 时间错误。

我 运行宁 windows 8.1 visual studio 2015 和最新的 directx sdk。 我希望我把问题解释得足够好,提前致谢。

尝试定义一个构造函数,然后在里面初始化你的数据。为避免在 *.h 文件中定义多个包含守卫或使用 #pragma once。

我在这个帖子上找到了答案

Crash after m = XMMatrixIdentity() - aligment memory in classes?

"In the Book "Introduction to 3D Game Programming with Directx 11" 来自 "Frank D. Luna" 它说:

不要将 XMMATRIX 用作 class 或结构的成员。始终使用 XMFloat4x4 并在需要时加载和存储。 “