简体   繁体   English

std :: cin中的运算符>>不匹配

[英]no match for operator >> in std::cin

I have a class employee 我有一个班级员工

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class employee
{
    public:

            double operator + (employee);
            istream& operator>> (istream&);

            employee(int);
            double getSalary();

    private:

           double salary;

};

int main()
{  
  employee A(400);
  employee B(800);

  employee C(220);

  cin>>C;

}

employee::employee(int salary)
{
    this->salary = salary;
}


double employee::operator + (employee e)
{
    double total;

    total = e.salary + this->salary;

    return total;    
}


double employee::getSalary()
{
    return this->salary;
}

istream& employee::operator>> (istream& in)
{
    in>>this->salary;

    return in;

}

I am trying to overload the input operator >> to read in the employee object but i am getting the following error 我试图重载输入运算符>>以读取employee对象,但出现以下错误

no match for operator >> in std::cin std :: cin中的运算符>>不匹配

What am i doing wrong ??? 我究竟做错了什么 ???

edit: I know how to do it through a friend function , i am now trying to learn how to do it through a member function 编辑:我知道如何通过一个朋友功能,我现在试图学习如何通过一个成员函数

You need to declare it thusly: 您需要这样声明:

class employee
{
public:
    friend std::istream& operator >> (std::istream& is, employee& employee);
}; // eo class employee

Implementation: 执行:

std::istream& employee::operator >> (std::istream& is, employee& employee)
{
    is >> employee.salary; // this function is a friend, private members are visible.
    return is;
};

As a side note, it's generally a bad idea to do using namespace std; 附带说明一下, using namespace std;通常不是一个好主意 using namespace std; within a header file. 在头文件中。

I know how to do it through a friend function , i am now trying to learn how to do it through a member function 我知道如何通过朋友功能做到这一点,我现在正在尝试学习如何通过成员功能做到这一点

You can't. 你不能

For a binary operator@ and objects A a and B b , the syntax a @ b will call either a non-member function of the form operator@(A,B) or a member function of the form A::operator@(B) . 对于二进制operator@和对象A aB b ,语法a @ b将调用或者是形式的非成员函数operator@(A,B) 形式的成员函数A::operator@(B) Nothing else. 没有其他的。

So to make std::cin >> C work it must be as a member of std::istream , but since you can't modify std::istream you can't implement operator>> as a member function. 因此,要使std::cin >> C正常工作,它必须作为std::istream的成员,但是由于您无法修改std::istream ,因此无法将operator>>作为成员函数实现。

(Unless you want to be weird and unconventional and write C << std::cin or C >> std::cin , but if you do that other programmers will hate you for being confusing and unconventional. Do not do this.) (除非您想变得怪异和非常规,并编写C << std::cinC >> std::cin ,但是如果您这样做,其他程序员会讨厌您因为混淆和非常规而感到讨厌。请不要这样做。)

It seems that we cannot declare operator << inside class declaration. 看来我们不能在类声明中声明运算符<<。 I've tried it and it is OK. 我已经尝试过了,没关系。

#include <stdio.h>
#include <iostream>
using namespace std;

struct foo {
    int field;
};

istream& operator >> (istream& cin, foo& a){
    cin >> a.field;
    return cin;
}

foo a;

main(){
    cin >> a;
}

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

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