无法初始化类型的变量 - 链接错误

But*_*ast 0 iphone opengl-es ios

我从另一个项目复制一些代码,我在以前的项目中工作正常但在新项目中我得到链接错误:

OpengLWaveFrontCommon.h:50:22:错误:无法使用'void*'类型的右值初始化'VertexTextureIndex*'类型的变量 VertexTextureIndex*ret = malloc(sizeof(VertexTextureIndex));

这个文件(OpengLWaveFrontCommon.h)是openGL iPhone项目的一部分:Wavefront OBJ Loader.https://github.com/jlamarche/iOS-OpenGLES-Stuff

我应该制作一些特殊的旗帜还是什么,因为它是C结构的?

#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

typedef struct {
    GLfloat red;
    GLfloat green;
    GLfloat blue;
    GLfloat alpha;
} Color3D;

static inline Color3D Color3DMake(CGFloat inRed, CGFloat inGreen, CGFloat inBlue, CGFloat inAlpha)
{
    Color3D ret;
    ret.red = inRed;
    ret.green = inGreen;
    ret.blue = inBlue;
    ret.alpha = inAlpha;
    return ret;
}


#pragma mark -
#pragma mark Vertex3D
#pragma mark -
typedef struct {
    GLfloat x;
    GLfloat y;
    GLfloat z;
} Vertex3D;

typedef struct {
    GLuint  originalVertex;
    GLuint  textureCoords;
    GLuint  actualVertex;
    void    *greater;
    void    *lesser;

} VertexTextureIndex;


static inline VertexTextureIndex * VertexTextureIndexMake (GLuint inVertex, GLuint inTextureCoords, GLuint inActualVertex)
{
    VertexTextureIndex *ret = malloc(sizeof(VertexTextureIndex));
    ret->originalVertex = inVertex;
    ret->textureCoords = inTextureCoords;
    ret->actualVertex = inActualVertex;
    ret->greater = NULL;
    ret->lesser = NULL;
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

Mid*_* MP 5

问题原因:

malloc()返回一个类型的指针void *,你需要输入类型转换为相应的数据类型.

malloc返回指向已分配空间的void指针,如果没有足够的可用内存,则返回NULL.要返回指向void之外的类型的指针,请在返回值上使用类型转换.返回值指向的存储空间保证适当地对齐,以存储具有小于或等于基本对齐的对齐要求的任何类型的对象.

参考malloc()

修复问题:

VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));
Run Code Online (Sandbox Code Playgroud)