Multiple datastreams created at runtime

I have a code producing multiple objects of a class "Item" at runtime, and would like to create a datafile associated with each. These files need to be written to intermittently during runtime, that is, I can't finish writing one before starting to write to the next, as the Items are producing data simultaneously. It seems therefore that I need a separate datastream associated with each Item.

I'm creating and indexing the Items with the following code snippet:

1
2
3
4
5
	Item *temp = new Item();
	population.push_back(*temp);
	delete temp;

	population[(population.size()-1)].index = population.size()-1;

where population is a vector of Items.

Any ideas on how to create multiple datastreams in this context?

I've tried a few things:
1) to create a similar vector of ofstream objects, but the code doesn't compile;
2) to initialise an ofstream in the Item constructor, but this understandably doesn't work because of the scope;
3) to create an ofstream as a member object of Item, but this also doesn't compile (whether set as private or as public).
Last edited on
A stream cannot be copied. It makes not much sense to create an item with new and delete it afterwards.

So make the stream part of Item and then emplace it to the vector:

http://www.cplusplus.com/reference/vector/vector/emplace_back/

population.emplace_back(Item());

Note that the default copy constructor cannot be used with stream as member. You need to provide one.
Topic archived. No new replies allowed.