2013-08-29 76 views
0

我想转发在头文件中声明结构。结构向前声明错误:使用不同类型的Typedef重新定义

struct GLFWvidmode; 

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

在CPP文件I包括与定义的外部报头...

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

...其中误差“重新定义的typedef具有不同类型( '结构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; 

我不能在这样的情况下转发声明吗?

+0

是的,我意识到后,我发表了评论,并从此抹去。 – Anycorn

+0

@AdamS:那么你不能避免包括头... –

回答

6

GLFWvidmode不是一个结构,它是一个typedef。你不能转发 - 声明一个typedef。谁选择使用无名结构做出了糟糕的设计决定。

+0

我能够改变外部头,因此所有出现的typedef结构都是合法的结构,感谢提示。 – Appleshell

3

我想提一提,GLFWvidmode是一个typedef名匿名结构..如果你有意要转发声明结构,那么你应该名签总是添加到结构,同时声明结构为:

typedef struct tagname1{ 
    some members...; 
    }tagname2; 

注DAT tagname1tagname2可以是相同的(可以用在两个地方tagname1tagnameGLFWvidmode)..而现在,因为该结构现在有一个标记名(它不是匿名的了),你可以参考它的向前声明。

一个匿名结构不能用于前向声明,因为没有标记名来引用.. :)希望它有帮助。

相关问题