结构前向声明错误:Typedef使用不同类型重新定义
Struct forward declaration error: Typedef redefinition with different types
我想在头文件中转发声明一个结构。
struct GLFWvidmode;
class DesktopVideoMode {
private:
const GLFWvidmode *videomode;
public:
DesktopVideoMode(const GLFWvidmode *videomode);
...
在cpp文件中,我包含了带有定义的外部标头。。。
#include "DesktopVideoMode.hpp"
#include <GLFW/glfw3.h>
其中发生错误"不同类型的Typedef重定义('struct GLFWvidmode'vs'GLFWvidmode')":
typedef struct
{
/*! The width, in screen coordinates, of the video mode.
*/
int width;
/*! The height, in screen coordinates, of the video mode.
*/
int height;
/*! The bit depth of the red channel of the video mode.
*/
int redBits;
/*! The bit depth of the green channel of the video mode.
*/
int greenBits;
/*! The bit depth of the blue channel of the video mode.
*/
int blueBits;
/*! The refresh rate, in Hz, of the video mode.
*/
int refreshRate;
} GLFWvidmode;
在这种情况下,我不能转发申报吗?
GLFWvidmode
不是一个结构,它是一个typedef。不能转发声明typedef。无论是谁选择使用未命名的结构,都做出了糟糕的设计决策。
我想提到GLFWvidmode
是匿名结构的typedef名称。。如果您有意转发声明该结构,那么在将该结构声明为时,应始终向该结构添加一个名称标记
typedef struct tagname1{
some members...;
}tagname2;
注意,数据tagname1
和tagname2
可以相同(您可以在这两个位置使用tagname1
、tagname
或GLFWvidmode
)。。现在,由于结构现在有一个标记名(它不再是匿名的),您可以引用它进行正向声明。
和yes匿名结构不能用于正向声明,因为没有标记名可引用..:)希望它能有所帮助。
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用模板类的自定义列表类型中的内存泄漏
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 自定义引用类型
- 在Qt中注册自定义元类型的别名类型
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 特征中的自定义标量类型
- 在点云库 (PCL) 中使用自定义点类型的问题
- 为什么即使直到最后才定义实际类型,也可以将依赖名称视为完整
- 使用用户定义的类型 UDT 实例化 std::atomic<>。如果 UDT 具有虚函数,则 l 墨水将失败。为什么?
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 如何声明具有在声明下定义的类型的参数的函数?
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- 虚函数如何工作,分配后新的返回类型会发生什么?
- 内置类型与用户定义的类型 C++
- 从自定义数据类型向量中删除重复元素
- 定义新的优化类型
- 如何获取当前持有的变量类型,并定义该类型的新变量
- PyDev外部调试与新的python类型定义
- 如何使用c++ 11风格的强类型定义创建一个新的基本类型