![]() |
|
#1
|
|||
|
|||
STL troubles saving and findingHello all,
I'm experiencing a few problems saving some STL data for a code my boss wanted written. The data is saved (I think) but when read back in it's all blank - though the correct number of objects are loaded. The initial employees are added and displayed fine, it's just the saving that's the problem. CPP / C++ / C Code:
No errors are appearing and as far as I can tell the data is being saved. I'm also troubled with the find and sort commands. All the examples I've seen create a simple list such as list<int> theList, and that's easy to sort. But how can I search on a specific class variable (such as employee number)? Many thanks, Clive. |
|
#2
|
||||
|
||||
|
Quote:
In attempting to read back in your employee data, you seem to be trying to create a new employee object without calling a constructor! While this may in fact be possible (though I can't think of a good reason to do so, and for complicated objects it could be prohibitive), your code does something very different. When saving, you are actually writing to file the bytes that describe the iterator p, not the bytes that describe the employee object pointed to by p. Now, when reading back in to the iterator r, you are retrieving the iterator information; that is, primarily, the addresses of the employee objects that used to reside in the list outStaff. However, when clearing the list, you have destroyed those employees, and the addresses you just read back in don't point to valid employee objects any more. See the code example at the end of this post, which uses two lists and does not clear the first, to demonstrate what I'm talking about. Then uncomment the code where we clear the first list, and let the fun ensue! Rather than try to desribe how to go about doing what you want with this technique, I recommend that you design your employee class so as to let the object itself manage the save and load. Perhaps, add to the employee class the following: 1. constructor that takes an array of bytes (char[]) and a size (# bytes) as arguments 2. serialize() or toBytes() or some such method that returns a char[]. Let the employee object decide what data needs to be saved, and then load it in the constructor itself. Quote:
So, the quickest way is to simply overload operator< for employee objects. CPP / C++ / C Code:
Ask any further questions, and maybe give this another try. I'll be happy to help out again (assuming I have even helped at all now) if I can. Matthew Here's the example code I mentioned: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
|
Thanks for this. Very much a newbie to all this and so confusion levels are still high but gives me a starting point.
Clive. |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The