|
-
September 11th, 2008, 07:41 AM
#1
Binary IO with Objects and Vectors
I'm getting weird run-time errors from Netbeans with the following code:
Code:
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class Data {
private:
double value;
vector<int> listOfInts;
public:
Data(double);
void Insert(int);
void ShowVector();
double GetValue(){ return value; }
};
Data::Data(double v)
{
value = v;
}
void Data::Insert(int i)
{
listOfInts.push_back(i);
}
void Data::ShowVector()
{
vector<int>::iterator it;
for(it = listOfInts.begin(); it != listOfInts.end(); it++){
cout << (*it);
}
cout << endl;
}
bool load(Data *d)
{
ifstream fin("test.bin", ios_base::in | ios_base::binary);
if (!fin.is_open()){ return false; }
fin.read((char*)(d), sizeof(Data));
fin.close();
return true;
}
bool save(Data *d)
{
ofstream fout("test.bin", ios_base::out | ios_base::binary | ios_base::trunc);
if (!fout.is_open()){ return false; }
fout.write((const char*)(d), sizeof(Data));
fout.close();
return true;
}
int main() {
Data x(4.5);
Data *y = new Data(7.8);
cout << x.GetValue() << endl;
x.Insert(1);
x.Insert(2);
x.Insert(3);
x.Insert(4);
save(&x);
cout << y->GetValue() << endl << "Vector : ";
y->ShowVector();
load(y);
cout << y->GetValue() << endl << "Vector : ";
y->ShowVector();
delete y;
return 0;
}
/cygdrive/d/Program Files/NetBeans 6.1/cnd2/bin/dorun.sh: line 103: 2132 Aborted (core dumped) "$pgm" "$@"
My actual project (far too much code to post here), which uses several vectors, one of which is of pointers to abstract classes, complains even more at run-time with the following:
handle_exceptions: Error while dumping state (probably corrupted stack)
/cygdrive/d/Program Files/NetBeans 6.1/cnd2/bin/dorun.sh: line 103: 2128 Segmentation fault (core dumped) "$pgm" "$@"
Is this a cygwin issue, my OS (Win2k) or something code related?
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
|