The following code compiles clean, and executes without error, however when the objects are printed out, they appear to be empty. The datafile is just a big list of numbers, nothing fancy. Im looking for somebody to tell me what im doing wrong, how to fix it, and if there is a faster way than copy to fill my vector.
Code:
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
class Bob
{
public:
int a;
int b;
int c;
int d;
int e;
int f;
Bob(void);
Bob(int,int,int,int,int,int);
~Bob(void);
friend std::ostream operator << (std::ostream, Bob);
friend std::istream operator >> (std::istream, Bob);
};
Bob::Bob()
{
a=0;
b=0;
c=0;
d=0;
e=0;
f=0;
}
Bob::~Bob()
{
}
Bob::Bob(int q, int w, int s, int r, int t, int y)
{
a = q;
b = w;
c = s;
d = r;
e = t;
f = y;
}
std::ostream operator << (std::ostream mystream, const Bob theobject)
{
mystream << theobject.a << " " << theobject.b << " " << theobject.c << " "
<< theobject.d << " " << theobject.e << " " << theobject.f;
mystream << std::endl;
return mystream;
}
std::istream operator >> (std::istream mystream, Bob theobject)
{
mystream >> theobject.a;
mystream >> theobject.b;
mystream >> theobject.c;
mystream >> theobject.d;
mystream >> theobject.e;
mystream >> theobject.f;
return mystream;
}
void main()
{
string filename = "test.txt";
ifstream datafile;
datafile.open(filename.c_str());
vector<Bob> Bobs;
copy( ( istream_iterator<Bob> (datafile) ),istream_iterator<Bob>(),back_inserter(Bobs));
cout << Bobs.size() << " Records Read!" << endl << endl;
cout << "The first bob contains: " << Bobs[0] << endl;
cout << "The next bob contains: " << Bobs[1] << endl;
cout << "The next bob contains: " << Bobs[2] << endl;
}