2013-08-29 73 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)..而現在,因爲該結構現在有一個標記名(它不是匿名的了),你可以參考它的向前聲明。

一個匿名結構不能用於前向聲明,因爲沒有標記名來引用.. :)希望它有幫助。

相關問題