简体   繁体   English

C ++类中的运算符重载函数

[英]Operator overloading function in class c++

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. 现在,我希望以Class格式编写该函数。 So I declared the typedef data and record variables in Header File called ("Headers.h") 所以我声明了typedef数据,并在名为(“ Headers.h”)的头文件中记录了变量

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

Now I wrote a class. 现在我写了一堂课。 FileHandler1.h 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 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 错误:“ std :: istream&FileHandler1 :: operator >>(std :: istream&,record_t&)”必须正好包含一个参数

How can I fix this bug ? 如何解决此错误?

A binary operator, such as operator>> , can be implemented as either a non-member function or a member function. 二进制运算符(例如operator>> )可以实现为非成员函数或成员函数。 When it is implemented as a non-member function, it takes two arguments, let's say T and U . 当将其实现为非成员函数时,它需要两个参数,例如TU 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. 当将其实现为成员函数时,它采用一个参数-仅U且左操作数是其成员所属类的类型。

That is, doing t >> u , where operator>> is a member of T , calls t.operator>>(u) . 也就是说,执行t >> u ,其中operator>>T的成员,调用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. 在这里,即使>>的左操作数不是该类型,您仍在尝试使operator>>成为FileHandler1的成员。 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. 如果允许您,则该运算符必须是istream的成员,但这并不由您决定,因为它是标准类型。 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: 如果您确实FileHandler1operator>>成员,则必须使用单个参数声明它:

FileHandler1& operator >> ( U& right_operand );

And then you must use it with a FileHandler1 object as the left operand: 然后,必须将其与FileHandler1对象一起用作左操作数:

FileHandler1 fh;
U u;
fh >> u; // Equivalent to fh.operator>>(u)

The input operators in classes is for when a class instance is the input stream. 类中的输入运算符适用于类实例输入流的情况。 Eg 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. 这似乎不是您想要的,因此您必须将输入运算符保留为独立功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM