我已经研究过我的问题的合理答案。我正在做一个大学项目,但我没有在其他代码中找到的字符串包含错误,我需要一些能给我一个关于正在发生的事情的提示。
错误:
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() {}`
答案 0 :(得分:0)
请参阅const PhoneCall &Call
的函数标题中的operator>>
,并且在函数内部,您正在更改const
对象,请尝试删除此const
friend istream &operator >> (istream &input, PhoneCall &Call) {
input >> Call.phoneNumber >> Call.length;
return input;
}