操作员<<不匹配

no match for operator <<

本文关键字:lt 不匹配 操作员      更新时间:2023-10-16

我正在用Deitel的书学习c++。我试图编译操作符重载的例子,但它失败了。你能给我解释一下吗?下面是代码:

类原型:

// 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

类定义:

// 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>>()
测试功能:

// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators
#include <iostream>
#include "phonenumber.h"
using namespace std;
int main(int argc, char *argv[])
{
    PhoneNumber phone;  // create object phone
    cout << "Enter phone number in the form (123) 456-7890:" << endl;
    // cin >> phone invokes operator>> by implicitly issuing
    // the non-member function call operator>>(cin, phone )
    cin >> phone;
    cout << "The phone number entered was: ";
    // cout << phone invokes operator<< by implicitly issuing
    // the non-member function call operator<<( cout, phone)
    cout << phone << endl;
    return 0;
}

当我尝试构建它时,我得到一个错误消息:

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;
                                                                   ~~~~^~~~~~~~~~~~~~