Please bear with me as I am awful with pointers. My instructor gave us an assignment whose main focus is inheritance. We have an 'item' class that has 2 classes derived from it, discountItem and taxableItem, the main difference being that they add a tax and a discount to their respective price variables.

Our teacher gave us a skeleton app that we have to use, which has a (STL) list of item pointers, which are supposed to "be allowed to point to an item, discountItem or taxableItem'. Its declared in main and each of the application's functions carry it as a parameter. The list instantiation is as follows:

list<item*> List;

We are reading in data from a file through a function, and its simple enough to read the data into its respective ADT because the istream is overloaded, so I can simple do file >> regItem, file >> discountItem, etc., but I cannot figure how to insert each of these items into the list because the list is instantiated as pointers. I can't do

alist.insert(i, regItem);

because its a list of pointers apparently! I'm going to go out on a limb and assume it has something to do with iterators, as some of the functions have an iterator itr as a parameter as well.

Any help at all to help me at least get started in getting the data into a transportable format would be awesome!

PS: I already wrote out the derived classes, etc, and we have to do simple things with the menu driven application such as add to the list, remove, view, change, print, etc. (all with the list as a parameter obviously), but I can't go any further until I actually know how to get the data into the list to begin with!

Nevermind, I got it! Its expecting a memory location, so I had to use &regItem! Thanks anyways! Still got to work on the rest of it and figure out how to utilize iterators to go through the list for a 'find function' haha!

If you haven't already figured it out, &regItem produces the same pointer (address of the regItem object in [virtual] memory) each time you use it, so you may be filling your list with identical pointers, whose values then will be whatever you most recently assigned into regItem.

Instead you should dynamically allocate regItems as you need them, and dereference them to input the data (and output whatever you need to output):

RegItemSpecificType *regItem = new RegItemSpecificType;
file >> *regItem;
alist.insert(regItem);
...
cout >> *alist_iter >> endl;
This article has been dead for over six months. Start a new discussion instead.