I am trying to print the contents of my map <Object, int>
. Here is the code in question:
void Inventory::print_initial_inventory()
{
for(auto p : inventory) {
std::cout << p.first << " : " << p.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Inventory& inv)
{
outstream << inv.first << "\nNumber of parts: " << inv.second << std::endl;
return outstream;
}
I know the problem is at p.first
? because std::cout
doesn't know how to print an object, so I tried to overload the operator<<
, but I'm not sure how to do it. Can anyone point me in the right direction?
EDIT Here's how I've tried the problem again. I was suggested to pass the key type to the operator<<
overload. Here is my code now:
void Inventory::print_initial_inventory()
{
for(auto x : inventory) {
std::cout << x.first << " : " << x.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Auto_Part& inv)
{
outstream << "Type: " << inv.type << "\nName: " << inv.name << "\nPart Number: " << inv.part_number << "\nPrice: $" << inv.price << std::endl;
return outstream;
}
I'm still getting an invalid binary expression error pointing to x.first
.
Aucun commentaire:
Enregistrer un commentaire