C ++ declare data of an array as parameter

1

Let's say I have this:

string opciones[] =
    {
        "Registrar estudiante",
        "Consultar estudiante",
        "Eliminar estudiante",
        "Atras"
    };

and I have a function like this:

construir(string titulo, string opciones[],int n)

and then I leave it like this:

construir("Elija una opcion:", string opciones[],4)

I can fill the title directly, and the variable n, but the parameter of the arrangement I put it like this:

construir("Elija una opcion:",{"Registrar","Consultar","Eliminar","Atras"},4)

And this is thrown by the compiler:

warning: extended initializer lists only available with std c++11 or std gnu++11

error: cannot convert '<brace-enclosed initializer list>' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '2' to 'int construir(std::__cxx11::string, std::__cxx11::string*, int)'|

I would like to know if you can directly declare the data in the parameters of the function as I did with the other two.

    
asked by Malthael 22.02.2017 в 18:54
source

2 answers

2

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 ...: '(

    
answered by 23.02.2017 / 09:00
source
1

You can use vectors if you pass it as a reference:

void construir(string titulo, const vector<string> &opciones)
{
    int n = opciones.size();
    // ...
}

And then you can use:

construir("Elija una opcion:", {"Registrar", "Consultar", "Eliminar", "Atras"});

It is also necessary to clarify that to use this you need C ++ 11 .

    
answered by 23.02.2017 в 06:15