typedef struct出错

时间:2017-01-05 17:05:39

标签: c++ struct typedef

以下代码会导致多个错误

typedef struct
{
    char name[20];
    int vertices_qty;
    int polygons_qty;
    Vector3D vertex[MAX_VERTICES];
    Triangle polygon[MAX_POLYGONS];
    TexCoord mapcoord[MAX_VERTICES];
    int id_texture;
    obj_type, *obj_type_ptr;
}

特别是最后一行:

obj_type, *obj_type_ptr;

当我用鼠标悬停在obj_type上时,它显示为:

  

此声明没有存储类或类型说明符

还有一条警告:

  

未标记的'struct'声明,没有符号

所有这些都在commons.h头文件中,该文件为使用openGL的程序定义了几个结构,例如vector2dvector3dmaterial等等。 / p>

我需要做些什么才能使错误消失?

2 个答案:

答案 0 :(得分:7)

你可能想要

typedef struct
{
    char name[20];
    int vertices_qty;
    int polygons_qty;
    Vector3D vertex[MAX_VERTICES];
    Triangle polygon[MAX_POLYGONS];
    TexCoord mapcoord[MAX_VERTICES];
    int id_texture;
}   obj_type, *obj_type_ptr;

由于obj_typeobj_type_ptr是新类型,因此必须在typedef struct{...} TYPE_HERE;的结束括号后指定。请注意,在C ++中,您不需要typedef,您只需定义

即可
struct obj_type{...};

然后按原样使用

obj_type foo;  // object of type obj_type
obj_type* foo_ptr; // pointer to obj_type

答案 1 :(得分:2)

简单地给最后两个有效类型。该错误意味着您指定了一个不具有现有类型或未指定任何类型的变量。 所以,根据你的解决方案:

typedef struct
{
    char name[20];
    int vertices_qty;
    int polygons_qty;
    Vector3D vertex[MAX_VERTICES];
    Triangle polygon[MAX_POLYGONS];
    TexCoord mapcoord[MAX_VERTICES];
    int id_texture; //Fine until the next line
    obj_type, *obj_type_ptr; //Compiler says: "Where is the type D:?"
}

只需在结构之前声明类型(不知道你的意思):

struct obj_type{...}//I'm a nice struct

将结构更改为:

 typedef struct
{
    char name[20];
    int vertices_qty;
    int polygons_qty;
    Vector3D vertex[MAX_VERTICES];
    Triangle polygon[MAX_POLYGONS];
    TexCoord mapcoord[MAX_VERTICES];
    int id_texture;
    obj_type *obj_type_ptr; //Now it's correct
}
相关问题