2014-10-08 154 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')。