Syntax error: missing ';' before '{'

I wrote a simple class with two properties (arrays). I try to initialize the entire array element to 0 or NULL, but the compiler (vC ++ 2010) throws me errors.

class Marina{ public: char port[100]; char bport[25]; Marina(){ this->port = {0}; this->bport = {0}; } }; 

I also tried a simple instruction like this:

 class Marina(){ public: char port[100] = {0}; char port[25] = {0}; }; 
0
source share
3 answers

You need the following:

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

This initializes both arrays with the total number of zeros.

In C ++ 11, you can define non-static member variables at the declaration point, so you can do this:

 class Marina { public: char port[100] = {0}; char bport[25] = {0}; }; 
+4
source
 Marina(){ //std::fill is in <algorithm> std::fill (port, port + 100, 0); std::fill (bport, bport + 25, 0); 

This piece of code is missing. There is no trailing brace! I replaced your tasks with one that will work as long as there is a bracket.

In addition, your code will still not compile this way, since initialization must be done in the list of initializers:

 Marina() : port ({0}), bport ({0}) {} 
+1
source

Your curly braces do not match. You have one more open parenthesis than closed parentheses. The code with inconsistent braces is poorly formed. Unfortunately, compiler messages in response to a missing bracket are not always the clearest. How does the compiler know which open bracket you forgot to close with a close bracket?

0
source

Source: https://habr.com/ru/post/916319/


All Articles