Yeah that looks like memory corruption/bad pointers to me. I see you are using the ID as the index into the file.
Yeah that looks like memory corruption/bad pointers to me. I see you are using the ID as the index into the file.
Last edited by abachler; 02-12-2008 at 04:49 PM.
Hehehe... I'm not sure I agree with that. It's always better to be type-safe rather than using generic pointers. In C++, we rarely need the void* anymore and we should avoid it too IMHO.
The templates are indeed better than the generic void*, but that's if the OP has learned to use them yet. I don't know.
Now that's even better!yes Next and Prev should be LINK*
hmmmm . ok.... o_0 I don't know WTF are you talking about but I need some help here. ^^
Well, let's think about it here.
First, your operator >> is not supposed to ask for data. That's your main program's doing. The object should just serialize itself. And it should probably do that in such a way that it writes a total of sizeof(yourobject) bytes so it seek alright.
You need to separate code for object and program.
Then you realize that IDs can be stored in any order, right? I can type in 1000 for the first, and then 10 for the second. The program would seek to 1000 * sizeof(myobject) and expect the data to be there, but it isn't.
What you need is an index. A map, perhaps. Map id to position in file.
Write that contents of the map to the file too when you close the program as an index, and load the index when the program starts.
Then the problem should be fixed.
:| The problem is that you are assuming I'm an expert. I"m just a newbie so what you just said right there doesn't make any sense to me whatsoever.
I think I was perfectly honest with you. Try to think into terms what happens if you enter 1000 for ID, for example. How will the program react? Use a debugger if you must. Se why you get what you get.
You need an index. Or you can simply make the ID automatic for every object. Or omit it completely.
Ah, it's your logic in circle's operator >> that's broken. Think about the method again and what it does, and why it can't possibly work the way you want it to.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Like everyone said i let the loop generate the ids, but still not working:
Here's an output example:Code:ostream& operator <<(ostream& output, const Circle& aCircle) { output << aCircle._circleId << " " << aCircle._circleRadius; return (output); } istream& operator >>(istream& input, Circle& aCircle) { int quantity; cout << "\nHow many circles do you want to add "; input >> quantity; for (int i = 0; i < quantity; ++i) { aCircle._circleId = i; input >> aCircle._circleRadius; } return (input); }
http://i29.tinypic.com/27zbz43.jpg
I do not see where you "add" Circles.Like everyone said i let the loop generate the ids, but still not working
You always work with the same variable - overwriting it on each loop iteration
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Right, let's try this again. Perhaps this time without flaming.
You still haven't thought about >> and its purpose enough. Moreover, you haven't thought about the implementation and what it does enough.
Consider this: a variable holds one item of data. (The data can be large and complex, depending on the type of the variable.) >> should fill one variable. Consider what data a single Circle object holds, and what your >> tries to do.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law