【问题标题】:C2679 binary '>>': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)C2679 二进制“>>”:未找到采用“const std::string”类型右侧操作数的运算符(或没有可接受的转换)
【发布时间】:2016-08-23 12:04:54
【问题描述】:

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

错误

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() {}`

【问题讨论】:

    标签: visual-c++


    【解决方案1】:

    看,operator&gt;&gt; 的函数头中有const PhoneCall &amp;Call,而在函数内部,你正在更改const 对象,请尝试删除此const

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

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 1970-01-01
      • 2018-04-22
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多