We are going to analyze the error you have obtained:
warning: extended initializer lists only available with std c++11 or std gnu++11
The first sentence of the error tells you (the translation is mine) that the extended initialization lists are only available from the C ++ 11 standard.
If you have given this error, it is possible that your compiler has capacity for the C ++ 11 standard but the option is not activated. Try to pass std=c++11
as a compilation option and let you use the ...
Extended initialization lists.
They are a new C ++ feature introduced in the C ++ 11 standard, allowing (almost) anything enclosed in braces ( {
and }
) to be considered a list. Specifically, a series of data of the same type, separated by a comma, enclosed in braces, will be translated as the template std::initializer_list
.
This template contains an arbitrary number of values of the same type, but is not an array . If you want to use it in your function construir
, you should do it in the following way:
construir(string titulo, initializer_list<string> opciones);
In this way, with C ++ 11 or higher, the following call would be correct:
construir("Elija una opcion:",{"Registrar","Consultar","Eliminar","Atras"});
It would not be necessary to pass the size of the list (you passed a 4) because the compiler counts the elements and you can consult them by means of the member function std::initializer_list::size
.
Extended initialization lists to build collections.
As you already mentioned Dementor 1 , it is possible to go to your function construir
a container and it will accept an initialization list. This is because these types of objects (containers) have a constructor that accepts initialization lists.
If you do not have a C ++ 11 compiler or higher ...
... forget about passing an initialization list. But you have the option to use templates:
template <std::size_t TAMANYO>
void construir(std::string, std::string (&opciones)[TAMANYO])
{
for (std::size_t indice = 0; indice < TAMANYO; ++indice)
std::cout << "Construyendo " << opciones[indice] << '\n';
}
The function would be used [like this] :
std::string opciones[] =
{
"Registrar estudiante",
"Consultar estudiante",
"Eliminar estudiante",
"Atras"
};
template <std::size_t TAMANYO>
void construir(std::string, std::string (&opciones)[TAMANYO])
{
for (std::size_t indice = 0; indice < TAMANYO; ++indice)
std::cout << "Construyendo " << opciones[indice] << '\n';
}
int main()
{
construir("test", opciones);
return 0;
}
It is not necessary to pass the size parameter because the template detects it.
1 Go bad royo de nombre ...: '(