Operator Overloading in Class

I have taken this code From this website forum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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.

1
2
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")

1
2
typedef vector <string> record_t;
typedef vector <record_t> data_t;


Now I wrote a class.
FileHandler1.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _FILEHANDLER
#define _FILEHANDLER
#endif

class FileHandler1
{
	
	public : 

		
		istream& operator >> ( istream& ins, record_t& record );
		
		istream& operator >> ( istream& ins, data_t& data );
	
};

FileHandler1.cpp
1
2
3
4
5
6
7
8
9
10
11
#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 ?
Last edited on
You may not declare such operators as class members.
In this case How can I declare ?
As I see they were already declared as stand alone functions.
In stand alone function I can give Two arguments. But When I declared the same function as a class method, Why am I getting the error as " Must take exactly one argument " ????
Can you change my code to be working ?
Instance functions take an additional hidden parameter which is a pointer to the the instance to work with (the this pointer). It affects the signature of the function.

You could make the functions static.
Topic archived. No new replies allowed.