我从另一个项目复制了一些代码,并且在以前的项目中工作正常,但是在新项目中却出现了链接错误:
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;
}
问题原因:
malloc()
返回type的指针void *
,您需要将其类型转换为相应的data-type。
malloc返回一个指向已分配空间的空指针,如果没有足够的可用内存,则返回NULL。若要返回指向非void类型的指针,请对返回值使用类型强制转换。返回值所指向的存储空间可以保证适当对齐,以存储对齐要求小于或等于基本对齐要求的任何类型的对象。
参考malloc()
解决该问题:
VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句