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;