[英]Why can't I forward declare pointer structs?
我正在使用一个稍微模糊的声音库,称为SDL_mixer。 GCC在抱怨我向前声明了一个指针结构? 我究竟做错了什么?
Mix_Music **music; // music[2] must be a pointer to fit any random file
music = new Mix_Music[3];
music[2] = Mix_LoadMUS("fire.ogg");
GCC返回:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
xxx.cpp||In function 'int SDL_main(int, char**)':|
xxx.cpp|28|error: invalid use of incomplete type 'Mix_Music {aka struct _Mix_Music}'|
SDL_mixer.h|131|error: forward declaration of 'Mix_Music {aka struct _Mix_Music}'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
编辑:我需要动态分配该结构,因为可能会加载成千上万个文件。 堆栈内存不起作用。
在new Mix_Music[3]
,编译器需要知道每个对象有多大才能知道要分配多少空间。 前向声明不提供该信息。
您可能需要new Mix_Music*[3]
。 尽管堆栈分配可能有效,但std::array
或std::vector
会使情况变得更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.