0

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

OpengLWaveFrontCommon.h:50:22: 错误: 无法使用 'void *' VertexTextureIndex *ret = malloc(sizeof(VertexTextureIndex)) 类型的右值初始化 '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;
}
4

1 回答 1

5

问题原因:

malloc()返回 type 的指针void *,您需要将其类型转换为相应的数据类型。

malloc 返回指向已分配空间的 void 指针,如果可用内存不足,则返回 NULL。要返回指向非 void 类型的指针,请对返回值使用类型转换。返回值所指向的存储空间保证被适当地对齐,以存储任何类型的对象,其对齐要求小于或等于基本对齐的对齐要求。

参考malloc()

修复问题:

VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));
于 2013-10-30T14:54:13.160 回答