不匹配运营商<<

时间:2016-10-26 06:46:50

标签: c++ c++11

我正在和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;

请问您能解释一下错误的原因是什么?

1 个答案:

答案 0 :(得分:2)

你有一个简单的拼写错误:

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

具体做法是:

number.exchange < "-"

应该是:

number.exchange << "-"

编译器认为您正在尝试在两个字符串上使用<运算符。只需修理线路即可。

Live Demo!

我系统上给我的错误信息是:

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