c++ - 结构前向声明错误 : Typedef redefinition with different types

标签 c++ struct forward-declaration

我想在头文件中转发声明一个结构。

struct GLFWvidmode;

class DesktopVideoMode {
private:
    const GLFWvidmode *videomode;
public:
    DesktopVideoMode(const GLFWvidmode *videomode);
...

在 cpp 文件中,我包含了带有定义的外部 header ...

#include "DesktopVideoMode.hpp"
#include <GLFW/glfw3.h>

...发生错误“Typedef redefinition with different types ('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。选择使用未命名结构的人做出了糟糕的设计决定。

关于c++ - 结构前向声明错误 : Typedef redefinition with different types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501993/

相关文章:

c# - WCF是建立在套接字上的吗?

go - 使用 net/http 将结构值存储在每个请求 Golang 中

javascript - 为什么我不能在 try block 中定义之前使用 Javascript 函数?

c++ - 模板类前向声明

c++ - 未找到引用的符号

c++ - 简单的效率问题 C++(内存分配)..也许一些碰撞检测有帮助?

c++ - 性能 : better to inizialize variable or just fill it?

c++ - 带有前向声明的默认模板参数

C++ 静态成员在初始化后重新初始化

c++ - C(++) 结构力额外填充