簡體   English   中英

結構前向聲明錯誤:使用不同類型重新定義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>

...發生錯誤“ 使用不同類型('struct GLFWvidmode'與'GLFWvidmode')重新定義Typedef”

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;

注意dat tagname1tagname2可以相同(您可以在兩個地方都使用tagname1tagnameGLFWvidmode )。現在由於該結構現在具有一個tagname(不再是匿名的),您可以引用它進行正向聲明。

的,匿名結構不能用於正向聲明,因為沒有標記名可引用.. :)希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM