简体   繁体   English

为什么我不能转发声明指针结构?

[英]Why can't I forward declare pointer structs?

I am using a slightly obscure sound library called SDL_mixer. 我正在使用一个稍微模糊的声音库,称为SDL_mixer。 GCC is complaining about me forward declaring a pointer struct? GCC在抱怨我向前声明了一个指针结构? What am I doing wrong? 我究竟做错了什么?

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 returns: 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)) ===|

edit: I need the struct to be dynamically allocated, as thousands of files might be loaded. 编辑:我需要动态分配该结构,因为可能会加载成千上万个文件。 Stack memory won't do. 堆栈内存不起作用。

In new Mix_Music[3] the compiler needs to know how big each object is in order to know how much space to allocate. new Mix_Music[3] ,编译器需要知道每个对象有多大才能知道要分配多少空间。 A forward declaration doesn't give it that information. 前向声明不提供该信息。

You probably want new Mix_Music*[3] . 您可能需要new Mix_Music*[3] Although stack allocation might work, and std::array or std::vector would make things better. 尽管堆栈分配可能有效,但std::arraystd::vector会使情况变得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM