Show array of objects with vector class

0

How can I display objects created in an array on the screen? The objects I want to build have two parameters defined by default initialized in the default constructor. What should I do?

This is my code:

    std::vector <Garito> garitos;
    garitos.emplace(garitos.end());
    /*
    std::cout << funciones::mostrarGarito(garitos); //Funcion que muestra los parametros de Garito a través de std::cout, pero no compila
   */


 return (0);
}
    
asked by JACK1ETO 24.06.2018 в 13:03
source

2 answers

1
  

Function that shows Garito's parameters through std::cout , but does not compile

std::cout << funciones::mostrarGarito(garitos);

If the function mostrarGarito is the same as define in another of your questions :

void funciones::mostrarGarito(const Garito& g){
   std::cout <<"Nombre del Garito: "<<g.getNombre()<<std::endl; 
   std::cout <<"Direccion del Garito: "<<g.getDireccion()<<std::endl;
}

It is normal that you do not compile. Leaving aside that the type defined as a parameter is different, the function is returning nothing ( void ) and nothing is happening to print, the data output flow ( std::cout ) does not know how to interpret that nothing and the compiler refuses (correctly) to compile it.

To pass std::vector is to the data output flows you should create a function that accepts them, which is usually advised to charge the data injection operator ( << ):

template<typename T>
std::ofstream &operator <<(std::ofstream &o, const std::vector<T> &v)
{
    for (const auto &d : v)
        std::cout << d << '\n';
    return o;
}

The previous code will allow you to show data of a vector as long as the data contained in the vector has the injection operator in data flow, in your case Garito does not have the same:

std::vector<int> i{1,2,3,4,5};
std::vector<Garito> g{Garito{}, Garito{}, Garito{}};

std::cout << i; // Correcto
std::cout << g; // Incorrecto, no hay sobrecarga de << para Garito.

But that can be solved by overloading the corresponding operator also for Garito:

std::ofstream &operator <<(std::ofstream &o, const Garito &g)
{
    return (o << g.getNombre() << ' ' << getDireccion() << '\n');
}

This will allow you to do what you were looking for:

std::vector <Garito> garitos;
garitos.emplace(garitos.end());
std::cout << garitos;
    
answered by 25.06.2018 / 11:09
source
0

For future questions, think that the declaration of the function mostrarGarito can be quite important ... try to create questions with all the necessary information.

I understand that the function mostrarGarito has the following statement (or equivalent):

namespace funciones
{
  void mostrarGarito(Garito const&);
}

Well, you have a vector of booths and a function that only supports an object of type Garito ... the solution is to use a loop:

for(Garito const& garito : garitos)
{
  funciones::mostrarGarito(garito);
}

If you can not / do not let you compile with the C ++ 11 standard and you have to resort to the already archaic C ++ 98 you can, for readability, use indexes:

for(size_t i=0; i<garitos.size(); i++)
{
  funciones::mostrarGarito(garitos[i]);
}    
    
answered by 25.06.2018 в 08:24