[英]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 tagname1
和tagname2
可以相同(您可以在兩個地方都使用tagname1
或tagname
或GLFWvidmode
)。現在由於該結構現在具有一個tagname(不再是匿名的),您可以引用它進行正向聲明。
是的,匿名結構不能用於正向聲明,因為沒有標記名可引用.. :)希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.