I'm having trouble overloading >> in a fairly specific manner. When the program reads from a file, I want it to read the data into multiple classes. However, you can't pass more than 2 arguments to an istream overload. I've fiddled around with making one class a base class of the other, but I don't think that will work out either. Here is the code.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

class Purchase;

class Order {
public:
	string name;
	string address;
	vector<Purchase> items;
};

class Purchase{
public:
	string product_name;
	double unit_price;
	int count;
	Purchase() {}
	Purchase(string pn, double up, int c) :product_name(pn), unit_price(up), count(c) {}
};

istream& operator>>(istream& in, Order& o, Purchase& p)
{
	getline(in, o.name);
	getline(in, o.address);
	getline(in, p.product_name);
	in >> p.unit_price >> p.count;
	o.items.push_back(Purchase(p.product_name, p.unit_price, p.count));
	return in;
}

int main)
{
	string file;
	cout << "Enter file to read orders from: ";

	while (true) {
	cin >> file;
	ifstream is(file.c_str());
	if(!is) cout << "File does not exist.\n";
	else break;
	}

	istream_iterator<Order> ii(is);
	istream_iterator<Order> eos;
	vector<Order> orders(ii,eos);

This shows what I want to do, and the error is obviously in the istream operator overload. I'm not even sure if that overload will work correctly yet, but for now I'm focused on reading data into multiple user defined types. Can anyone explain how to do this? Thanks.

To further clarify, an Order can have multiple purchases. This is another roadblock I'm running into. Somehow I need to extract the information in such a way that when a Purchase is extracted, and the Order data along with it, the program will know to put that purchase data in the vector of purchases in Order if the Order data matches Order data that's already been extracted. (Example: John Doe of 117 Cherry Road (an Order) has 3 Purchases).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.