Help with storing the content of vector into txt

Hi there guys, I need some assistance from an assignment I'm working on. I need to store the contents of my vectors into a text file, I've tried using other methods I've found online but I can't seem to get it to work and I suspect it is due to the fact that my vector stores more than one variable and is linked to a class.

class food
{
~~
food (string name, double price) : name(name), price(price) {}~~
~~
void print()
{
cout << " " << name << "\tRM" << price << endl;
}
};

vector<food> orderpizza;

So basically in my vector it's gonna be:
orderpizza.push_back(food("Hawaiian ", 12.00));
orderpizza.push_back(food("Pepperoni", 12.00));

I've tried:

ofstream menusav("Menu.txt");
menusav << orderpizza[0].print();
menusav.close();

the error that shows is : error: no match for 'operator<<' in 'menusav << menu(std::vector<food>((*(const std::vector<food>*)(& orderpizza))), std::basic_string<char>(((const char*)"Pizza"), (*(const std::allocator<char>*)(& std::allocator<char>()))))'|

Any help would be appreciated, and I will provide more info on my program if you need it. Thanks.
If you were to change the print definition to:

1
2
3
4
5
6
7
8
9
class food
{
    ...
    void print( std::ostream& os ) const
    {
        os << " " << name << "\tRM" << price << '\n' ;
    }
    ...
};


Then you could use it like so:

1
2
    std::ofstream menusav("Menu.txt") ;
    orderpizza[0].print(menusav);
That is exactly what I was looking for, thank you so much ! I can apply that exact method for the rest of my program too, thank you !
Topic archived. No new replies allowed.