2012-11-29 84 views
0

使用枚舉當我嘗試編譯此代碼與Windows 8 SDK:C++編譯錯誤的C2275結構

typedef struct { 
    enum { red, blue, green } eColor; 
    /* other params here */ 
} StMyStruct; 

void Myfunction(StMyStruct *pst) 
{ 
    if (pst->eColor==StMyStruct.red) { 
     /* some code here */ 
    } 

} 

但我得到這個錯誤就行如果pst->易彩== StMyStruct.red

錯誤C2275: 'StMyStruct':非法使用這種類型作爲表達

不知道如何解決它?

我使用Windows SDK 7成功編譯此代碼,錯誤僅在Windows 8 SDK中發生。

回答

0

嘗試StMyStruct::red

此外,您不需要typedef,主要用於在C++中開發C - 時,可以直接引用結構和類名稱。

+0

::不解決問題,我得到相同的錯誤 – TheFrancisOne

+0

也許VC++需要該結構有一個名稱:試着用'struct StMyStruct {...};'然後'StMyStruct :: red'。 – rodrigo

+0

是的,這很有用,非常感謝! – TheFrancisOne

0

我不知道爲什麼,這與編譯SDK7但我認爲枚舉值是StMyStruct靜態成員因此應通過StMyStruct::

typedef struct { 
    enum { red, blue, green } eColor; 
} StMyStruct; 

void Myfunction(StMyStruct *pst) 
{ 
    if (pst->eColor==StMyStruct::red) { 
     /* some code here */ 
    } 
} 

解決完全編譯我與海灣合作委員會和Visual Studio。

+0

::沒有解決問題,我得到相同的錯誤 – TheFrancisOne

+0

嗯,不知道那麼,因爲它修復了我有權訪問的所有編譯器。 (現在已經用相同的結果嘗試了Comeau和Clang。) –