This is my standalone operator overloading function.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef vector <string> record_t;
typedef vector <record_t> data_t;
istream& operator >> ( istream& ins, record_t& record )
{
record.clear();
string line;
getline( ins, line );
stringstream ss( line );
string field;
while (getline( ss, field, '\t' ))
{
stringstream fs( field );
string f(""); // (default value is 0.0)
fs >> f;
record.push_back( f );
}
return ins;
}
istream& operator >> ( istream& ins, data_t& data )
{
data.clear();
record_t record;
while (ins >> record)
{
data.push_back( record );
}
return ins;
}
int main()
{
// Here is the data we want.
data_t data;
ifstream infile( "split.idx" );
infile >> data;
if (!infile.eof())
{
cout << "Fooey!\n";
return 1;
}
infile.close();
// Otherwise, list some basic information about the file.
cout << "Your CSV file contains " << data.size() << " records.\n";
return 0;
}
Here I've two Operator Overloading functions.
istream& operator >> ( istream& ins, record_t& record )
istream& operator >> ( istream& ins, data_t& data )
Now I wish to write the function in a Class format. So I declared the typedef data and record variables in Header File called ("Headers.h")
typedef vector <string> record_t;
typedef vector <record_t> data_t;
Now I wrote a class. FileHandler1.h
#ifndef _FILEHANDLER
#define _FILEHANDLER
#endif
class FileHandler1
{
public :
istream& operator >> ( istream& ins, record_t& record );
istream& operator >> ( istream& ins, data_t& data );
};
FileHandler1.cpp
#include "Headers.h"
#include "FileHandler1.h"
istream& FileHandler1:: operator >> ( istream& ins, record_t& record )
{
//Same set of code I posted initially
}
istream& FileHandler1:: operator >> ( istream& ins, data_t& data )
{
// Same set of code
}
Now I got below error.
error: ‘std::istream& FileHandler1::operator>>(std::istream&, record_t&)’ must take exactly one argument
How can I fix this bug ?
_FILEHANDLER) or names that contain two consecutive underscores. They are reserved to the implementation.