C++表达式必须是可修改的值

C++ expression must be a modifiable value

本文关键字:修改 表达式 C++      更新时间:2023-10-16
struct PLANE {FLOAT X, Y, Z; D3DXVECTOR3 Normal; FLOAT U, V;};
class PlaneStruct
{
public:PLANE PlaneVertices[4];
public:DWORD PlaneIndices;
void CreatePlane(float size)
{
    // create vertices to represent the corners of the cube
    PlaneVertices = 
    {
        {1.0f * size, 0.0f, 1.0f * size, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 0.0f},    // side 1
        {-1.0f * size, -0.0f, 1.0f * size, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 1.0f},
        {-1.0f * size, -0.0f, -1.0f * size, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 0.0f},
        {1.0f * size, -0.0f, -1.0f * size, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 1.0f},
    };
    // create the index buffer out of DWORDs
    DWORD PlaneIndices[] =
    {
        0, 2, 1,    // side 1
        0, 3, 2
    };
}
};  

这是我的"平面"结构代码,我只有一个问题,如果你看顶部它说PLANE PlaneVertices[4];然后在一个函数中,我想定义它,所以给它特定的值,但我得到以下错误: 表达式必须是可修改的值。请帮忙

在 C++ (2003) 中初始化,如 StructX var = { ... }; 只能在定义变量时使用。在代码中,平面顶点用于赋值表达式。那里不允许使用初始化语法。这是一个语法错误。

稍后,您将定义一个局部变量 PlaneIndices,该变量将在退出方法后被丢弃。

你不能像这样为PlaneVertices数组赋值,只有在使用 {} 表示法定义它来初始化它时才能使用它。尝试使用 for 循环将每个元素分配给数组的每个独立元素

编辑:为了响应您的评论,创建 PLANE 结构的实例并为其分配您希望它具有的值。然后PlaneVertices使用

    PlaneVertices[0] = // instance of PLANE struct you have just created

然后对数组中所需的其余 3 个 PLANE 实例重复此操作,将 1、2 和 3 个索引添加到 PlaneVertices 的 1、2 和 3 个索引中。为了充分说明,我将使用您提供的数据为您做第一个

    PLANE plane_object;
    plane_object.X = 1.0*size;
    plane_object.Y = 0.0; 
    plane_object.Z = 1.0*size; 
    plane_object.Normal = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
    plane_object.U = 0.0;
    plane_object.V = 0.0;
    PlaneVertices[0] = plane_object;

然后,您需要对要添加的每个平面重复此操作。也不要拿关于你的平面指数问题的其他答案。