这是我的独立运算符重载函数。
#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;
}
这里我有两个运算符重载函数。
istream& operator >> ( istream& ins, record_t& record )
istream& operator >> ( istream& ins, data_t& data )
现在我希望以 Class 格式编写函数。所以我在名为(“Headers.h”)的头文件中声明了typedef数据和记录变量
typedef vector <string> record_t;
typedef vector <record_t> data_t;
现在我写了一个类。文件处理程序1.h
#ifndef _FILEHANDLER
#define _FILEHANDLER
#endif
class FileHandler1
{
public :
istream& operator >> ( istream& ins, record_t& record );
istream& operator >> ( istream& ins, data_t& data );
};
文件处理程序1.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
}
现在我得到了以下错误。
错误:'std::istream& FileHandler1::operator>>(std::istream&, record_t&)' 必须只有一个参数
我怎样才能修复这个错误?