Reputation: 2797
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 ?
Upvotes: 0
Views: 1022
Reputation: 110658
A binary operator, such as operator>>
, can be implemented as either a non-member function or a member function. When it is implemented as a non-member function, it takes two arguments, let's say T
and U
. When it is implemented as a member function, it takes one argument - just U
- and the left operand is the type of the class it is a member of.
That is, doing t >> u
, where operator>>
is a member of T
, calls t.operator>>(u)
.
Here, you're trying to make operator>>
a member of FileHandler1
, even though the left operand of >>
is not of that type. If you were allowed, the operator would have to be a member of istream
, but that's not up to you because it's a standard type. You have to implement these functions as non-member functions.
If you do have an operator>>
member of FileHandler1
, it will have to be declared with a single argument:
FileHandler1& operator >> ( U& right_operand );
And then you must use it with a FileHandler1
object as the left operand:
FileHandler1 fh;
U u;
fh >> u; // Equivalent to fh.operator>>(u)
Upvotes: 3
Reputation: 409166
The input operators in classes is for when a class instance is the input stream. E.g. in your case:
FileHandler1 fh1;
fh1 >> something;
The above translates into:
fh1.operator>>(something);
This is the reason that member input operator only takes a single argument, which is what the error message tells you.
This doesn't seem to be what you want, so you have to keep the input operators as free-standing functions.
Upvotes: 0