mardi 14 avril 2015

C++11 Default member variable value + brace-enclosed initializer list

The code below works



#include <iostream>

using namespace std;

struct Formula {
int x;
};

int main() {
Formula f = {1};
return 0;
}


However, after adding the default value for the member variable, it fails.



#include <iostream>

using namespace std;

struct Formula {
int x = 10; // Initialize x with a default value 10.
};

int main() {
Formula f = {1}; // this line no longer works.
return 0;
}


The error is below:



g++ -std=gnu++11 exp07.cc
exp07.cc: In function ‘int main()’:
exp07.cc:10:17: error: could not convert ‘{1}’ from ‘<brace-enclosed initializer list>’ to ‘Formula’
Formula f = {1};

Aucun commentaire:

Enregistrer un commentaire