2014-10-08 220 views
-4

我試圖編譯.c文件,其中包括相應的.h這樣一個ISO C++編譯錯誤的C代碼

typedef struct myType 
{ 
    char* myString = "NOT SET"; 
    int myInt = -1; 
} 

運行「GCC -c myfile.c文件」引發了以下錯誤時, :

Error: ISO C++ forbids initialization of member 'myString' 
Error: making 'myString' static 
Error: invalid in-class initialization of static data member of non-integral type ‘char*’ 
Error: ISO C++ forbids initialization of member 'myInt' 
Error: making 'myInt' static 
Error: invalid in-class initialization of static data member of non-integral type ‘int’ 

所以我的問題是雙重的:首先,我爲什麼不能初始化值在我的頭的結構?其次,爲什麼我在編譯C代碼時遇到ISO C++錯誤?

+3

C或C++?包括你展示C++ btw的爭論。 – Deduplicator 2014-10-08 14:36:59

+2

請創建一個[最小工作示例](http://stackoverflow.com/help/mcve)/ [備用](http://sscce.org/)。 – Unapiedra 2014-10-08 14:39:15

+0

像這樣在'struct'中初始化東西是否合法?我認爲這是合法的,因爲C++ 11 ... – GingerPlusPlus 2014-10-08 15:01:41

回答

2

gcc當文件擴展名爲.C(大寫字母'C')時,會自動進入C++模式。確保您的C源文件的擴展名爲.c(小寫字母'c')。