与操作员不匹配<&书信电报; 我正在用Deitel的书学习C++。我试图编译运算符重载示例,但失败了。你能解释一下为什么吗?代码如下:

与操作员不匹配<&书信电报; 我正在用Deitel的书学习C++。我试图编译运算符重载示例,但失败了。你能解释一下为什么吗?代码如下:,c++,c++11,C++,C++11,类原型: // PhoneNumber class definition #include <iostream> #include <string> #ifndef PHONENUMBER_H #define PHONENUMBER_H class PhoneNumber { // Start class PhoneNumber friend std::ostream & operator<<( std::ostream &outp

类原型:

// PhoneNumber class definition
#include <iostream>
#include <string>
#ifndef PHONENUMBER_H
#define PHONENUMBER_H


class PhoneNumber
{ // Start class PhoneNumber
    friend std::ostream & operator<<( std::ostream &output, const PhoneNumber &number );
    friend std::istream & operator>>( std::istream &input, PhoneNumber &number);
private:
    // 3-digit area code
    std::string areaCode;
    // 3-digit exchange
    std::string exchange;
    // 4-digit line
    std::string line;
}; // End Class PhoneNumber

#endif // PHONENUMBER_H
//电话号码类定义
#包括
#包括
#ifndef电话号码
#定义电话号码
类电话号码
{//开始类电话号码
friend std::ostream&operator(std::istream&input,PhoneNumber&number);
私人:
//三位区号
字符串区域码;
//三位数交换
字符串交换;
//四位线
std::字符串行;
}; // 端类电话号码
#endif//PHONENUMBER\u H
类别定义:

// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber
#include <iomanip>
#include "phonenumber.h"

using namespace std;

// overloaded stream insertion operator; can't be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream & operator <<( ostream &output, const PhoneNumber &number )
{ // Start operator<<()
    output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;

    return output;  // enables cout << a << b << c;
} // End operator<<()

// overloaded stream extraction operator; can't be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber
istream & operator >>( istream &input, PhoneNumber &number )
{ // Start operator>>()
    // skip (
    input.ignore();
    // input area code
    input >> setw( 3 ) >> number.areaCode;
    // skip ) and space
    input.ignore( 2 );
    // input exchange
    input >> setw( 3 ) >> number.exchange;
    // skip dash(-)
    input.ignore();
    // input line
    input >> setw( 4 ) >> number.line;

    return input;   // enables cin >> a >> b >> c;
} // end operator>>()
//重载的流插入和流提取运算符
//类电话号码
#包括
#包括“phonenumber.h”
使用名称空间std;
//重载流插入算子;不可能
//一个成员函数,如果我们想用
//通过隐式发出
//非会员功能呼叫接线员>>(cin,电话)
cin>>手机;

行中有一个简单的输入错误:

output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;

输出您的运算符仅存在于.cpp文件中。你需要在.h中声明,以便你的main知道它。导致错误消息的直接原因是
“-”@songyuanyao在粘贴错误消息时一定有格式错误,我相信我已经更正了问题?是的,你已经更正了,只是对“不是‘运营商’的对手,银行有很多。我没有发现拼写错误。@Vladimir如果我的答案对你有用,请单击我答案旁边的复选标记来标记你回答的问题。
phonenumber.cpp:13: error: no match for ‘operator<<’ (operand types are ‘const char [2]’ and ‘const string {aka const std::__cxx11::basic_string<char>}’)
     output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
number.exchange < "-"
number.exchange << "-"
main.cpp:30:71: error: no match for 'operator<<' (operand types are 'const char [2]' and 'const string {aka const std::__cxx11::basic_string<char>}')
     output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
                                                                   ~~~~^~~~~~~~~~~~~~