gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:35:39 25 4
gpt4 key购买 nike

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

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() 返回一个类型为void *的指针,你需要将其类型转换为相应的数据类型。

malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object that has an alignment requirement less than or equal to that of the fundamental alignment.

引用 malloc()

问题修复:

VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));

关于ios - 无法初始化类型变量 - 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com