C2679二进制'>>':找不到运算符,它接受'const std :: string'类型的右手操作数(或者没有可接受的转换)

时间:2016-08-23 12:04:54

标签: visual-c++

我已经研究过我的问题的合理答案。我正在做一个大学项目,但我没有在其他代码中找到的字符串包含错误,我需要一些能给我一个关于正在发生的事情的提示。

错误

C2679   binary '>>': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)  PhoneCall2  c:\users\mano\documents\visual studio 2015\projects\phonecall2\phonecall2\phonecall.h   25

代码

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

class PhoneCall
{
public:
    PhoneCall();
    PhoneCall(string, int);
    ~PhoneCall();

private:
    string phoneNumber;
    int length;
    double static ratePerMinute;

    friend ostream &operator << (ostream &output, const PhoneCall &Call) {
        output << "Phone number :" << Call.phoneNumber<<"Length :"<<Call.length;
        return output;
    }

    friend istream &operator >> (istream &input, const PhoneCall &Call) {
        input >> Call.phoneNumber >> Call.length; \\right here.
        return input;
    }

    friend bool operator==(const PhoneCall &p1, const PhoneCall &p2);
    friend bool operator !=(const PhoneCall &p1, const PhoneCall &p2);
};
double PhoneCall::ratePerMinute = 0.00;

PhoneCall::PhoneCall() {
    phoneNumber = "";
    length = 0;
}

PhoneCall::PhoneCall(string c, int m) {
    phoneNumber = c;
    length = m;
}

bool operator==(const PhoneCall &p1, const PhoneCall &p2){
    return (p1.phoneNumber == p2.phoneNumber);
}
bool operator!=(const PhoneCall &p1, const PhoneCall &p2){
    return !(p1.phoneNumber == p2.phoneNumber);
}

PhoneCall::~PhoneCall() {}`

1 个答案:

答案 0 :(得分:0)

请参阅const PhoneCall &Call的函数标题中的operator>>,并且在函数内部,您正在更改const对象,请尝试删除此const

friend istream &operator >> (istream &input, PhoneCall &Call) {
    input >> Call.phoneNumber >> Call.length;
    return input;
}
相关问题