|
-
Jan 7th, 2003, 02:52 AM
#1
Thread Starter
Addicted Member
iostream using pointer
typedef TArray<CShape*> CShapes;
typedef TArrayIterator<CShape*> CShapesIterator;
CShapes *shapes = new CShapes(10, 0, 10);
void TDrawWindow::save()
{
ofstream os("c:\\abc.txt");
CShapesIterator i(*shapes );
while (i) {
CShape *p = i++;
os<<p;
}
}
is it correct or not?
if it is correct then how to retrive it from file
I have done so but it is not working.
void TDrawWindow::Open()
{
ifstream is("c:\\abc.txt");
shapes ->Flush();
unsigned numPoints;
is >> numPoints;
while (*numPoints--) {
CShape shape;
is >> shape;
shapes->Add(&shape);
}
Invalidate();
}
-
Jan 7th, 2003, 12:14 PM
#2
No, not correct. You're only saving the pointer value.
Unless there is a specific overload of << for TObject * or TShape *. If not you'll have to do
os << *i;
and overload the << operator for const TShape &.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2003, 11:42 PM
#3
Thread Starter
Addicted Member
Thanks
friend ostream& operator<< ( ostream& os, CShape& shape );
ostream& operator<< ( ostream& s, CLine& dt )
{
s << shape .pt1 << shape .pt2 << '\n';;
return s;
}
this is what I have done for direct access.
But How to overload for pointer
-
Jan 7th, 2003, 11:58 PM
#4
Frenzied Member
Originally posted by purusingh
Thanks
friend ostream& operator<< ( ostream& os, CShape& shape );
ostream& operator<< ( ostream& s, CLine& dt )
{
s << shape .pt1 << shape .pt2 << '\n';;
return s;
}
this is what I have done for direct access.
But How to overload for pointer
You dont have to overload for pointers. Just dereference p.
Z.
-
Jan 8th, 2003, 12:19 AM
#5
Thread Starter
Addicted Member
please show me how to use it in these functions
void TDrawWindow::save()
{
ofstream os("c:\\abc.txt");
CShapesIterator i(*shapes );
while (i) {
CShape *p = i++;
os<<p;
}
void TDrawWindow::Open()
{
ifstream is("c:\\abc.txt");
shapes ->Flush();
unsigned numPoints;
is >> numPoints;
while (*numPoints--) {
CShape shape;
is >> shape;
shapes->Add(&shape);
}
Invalidate();
}
-
Jan 8th, 2003, 04:22 AM
#6
Didn't I already?
Code:
while (i) {
CShape *p = i++;
os<<*p;
}
And make the prototype of the << operator
ostream& operator<< ( ostream& s, const CLine& dt);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 8th, 2003, 08:09 AM
#7
Thread Starter
Addicted Member
yes I did so but it is not working.
-
Jan 8th, 2003, 10:41 AM
#8
In that case my knowledge fails.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 8th, 2003, 11:46 AM
#9
Frenzied Member
In your Load function, you read the number of shapes saved. You never save that value in your save function.
Z.
-
Jan 8th, 2003, 11:48 AM
#10
Frenzied Member
Also, while(*numPoints--) should be while(numPoints--), since it isnt a pointer value.
Z.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|