为什么我不能转发声明指针结构?
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)) ===|
edit:我需要对结构进行动态分配,因为可能会加载数千个文件。堆栈内存不行。
在new Mix_Music[3]
中,编译器需要知道每个对象有多大,才能知道要分配多少空间。远期申报并不能提供这些信息。
您可能想要new Mix_Music*[3]
。尽管堆栈分配可能会起作用,但std::array
或std::vector
会使情况变得更好。
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- 指针或结构的矢量
- MSVC将仅移动结构参数解释为指针
- 如何访问指针结构的成员变量?
- 如何将变量从变量传递到指针结构对象
- 如何在函数中检索指针结构
- C :变量的点地址至指针结构成员的地址
- C++指针结构不会被删除
- 初始化指针结构 - 内存中的外观
- C++指针结构和多线程
- c++中指针结构的运算符重载
- 为什么我不能转发声明指针结构?
- C# 如何定义新的指针结构
- 如何处理大型指针结构
- 我怎么能写一个函数的定义在模板类中声明返回指针结构对象
- 我如何编程指针结构或类变量在c++
- 具有指针结构的类所需的析构函数
- 为什么在非指针结构上进行类型转换会出现语法错误
- 语法:做binary_search (STL)排序向量的指针结构
- /a.检测到Glibc.out: munmap_chunk():无效指针- c++结构体的动态数组