>我从另一个项目复制了一些代码,我在以前的项目中工作正常,但在新项目中我收到链接错误:
OpengLWaveFrontCommon.h:50:22:错误:无法使用类型为"void *"的右值初始化类型为"顶点纹理索引*"的变量 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;
}
问题原因:
malloc()
返回一个类型为 void *
的指针,则需要将其类型转换为相应的数据类型。
malloc 返回指向已分配空间的 void 指针,如果有,则返回 NULL 可用内存不足。返回指向其他类型的指针 比 void 时,对返回值使用强制转换的类型。存储空间 由返回值指向的保证适当对齐 用于存储具有对齐要求的任何类型的对象 小于或等于基本对齐的对齐。
参考 malloc()
修复此问题:
VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));