简体   繁体   中英

syntax error : missing ';' before '{'

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};



};

You need this:

Marina() : port(), bport() {}

This initializes both arrays with full of zeroes.

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};
};
Marina(){
//std::fill is in <algorithm>
std::fill (port, port + 100, 0);
std::fill (bport, bport + 25, 0);

This code segment is missing something. There's no ending brace! I replaced your assignments with something that will work as long as the brace is there.

Beside that, your code will still not compile that way as initialization has to be done in the initializer list:

Marina()
    : port ({0}), bport ({0}) {}

Your braces don't match. You have one more open brace than close braces. Code with mismatched braces is ill-formed. Unfortunately, compiler messages in response to a missing close brace are not always the clearest. How is the compiler to know which open brace you forgot to close with a close brace?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM