语法错误:缺少';'前 '{' [英] syntax error : missing ';' before '{'
本文介绍了语法错误:缺少';'前 '{'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个带有两个属性(数组)的简单类.我试图将所有数组元素初始化为0或NULL,但是编译器(vc ++ 2010)抛出错误.
I have written a simple class with two property(arrays). I am trying to initiliaize all the array element to 0 or NULL but the compiler(vc++ 2010) throw me errors.
class Marina{
public:
char port[100];
char bport[25];
Marina(){
this->port = {0};
this->bport = {0};
}
};
我也尝试过一个简单的语句,如下所示:
I have also tried a simple statement like this:
class Marina(){
public:
char port[100] = {0};
char port[25] = {0};
};
推荐答案
您需要这样做:
Marina() : port(), bport() {}
这将初始化两个都为零的数组.
This initializes both arrays with full of zeroes.
在C ++ 11中,您可以在声明时定义非静态成员变量,因此您可以这样做:
In C++11 you can define non-static member variables at the point of declaration, so you could do this:
class Marina {
public:
char port[100] = {0};
char bport[25] = {0};
};
这篇关于语法错误:缺少';'前 '{'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文