gpt4 book ai didi

c - 错误 : A compound literal of type not allowed

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:09 26 4
gpt4 key购买 nike

我有一个包含指向回调函数的指针的结构,如下所示:

typedef void    (*LOAD_CB)(resource* r);
typedef void (*UNLOAD_CB)(resource* r);

typedef void (*CREATE_CB)(void* i);
typedef void (*DESTROY_CB)(void* i);

typedef struct /*_resman_callbacks*/ //<-- causes error
{
LOAD_CB load;
UNLOAD_CB unload;
CREATE_CB create;
DESTROY_CB destroy;

} resman_callbacks;

然后我在运行时初始化这些函数,并将它们添加到包含其他回调函数的列表中:

register_extension(".my_file_extension", &(/*error appears here*/resman_callbacks){load, unload, create, destroy});

当我的结构没有名称(例如 _resman_callbacks)时,这不会导致错误,但是当在结构定义中提供名称时,我的 IDE 会显示错误“a compound literal of type “resman_callbacks” is not允许”。这不会导致任何运行时问题,也不会导致代码编译失败。这给我留下了两个问题

A) 考虑到我的代码仍然有效,这是否值得关注?

B) 为什么结构有一个名字导致出现错误?

我的 IDE 是 Visual Studio Express 2013。

编辑:增加了register_extension的声明和实现,以及其他相关数据。

//resman.h
void register_extension(char* file_ext, resman_callbacks* cb);

//resmain.c
typedef struct
{
char* ext; //extension associated with this resource
resman_callbacks* cb; //structure containing callback functions

} registered_extension;

typedef struct
{
registered_extension* reg_ext; //list of registered extensions
unsigned short ext_ct; //current number of registered extensions
unsigned short size; //maximum number of registered extensions
} ext_manager;

static ext_manager extman;

void register_extension(char* ext, resman_callbacks* callbacks)
{
if (extman.ext_ct == extman.size)
{
extman.size = extman.size * 2;
extman.reg_ext = realloc(extman.reg_ext, extman.size);
}

*(extman.reg_ext + extman.ext_ct) = (registered_extension) { ext, callbacks };
extman.ext_ct = extman.ext_ct + 1;
}

最佳答案

假设您在实际在代码中使用它们之前声明了以下函数:

void load(resource* r);
void unload(resource* r);
void create(void* i);
void destroy(void* i);

试试这个:

resman_callbacks cb = {load, unload, create, destroy};
register_extension(".my_file_extension", &cb);

关于c - 错误 : A compound literal of type not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227455/

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