关于操作符重载的问题

discory discory 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;

#include <iomanip>
using std::setw;

class PhoneNumber
{
friend ostream &operator <<( ostream&, const PhoneNumber & );
friend istream &operator >>( istream&, const PhoneNumber & );

private:
char areaCode[ 4 ]; //3-digit area code and null
char exchange[ 4 ]; //3-digit exchange and null
char line[ 5 ]; //4-dighit line and null
};

//Overloaded stram-insertion operator( cannot be
//a member function if we would like to invoke it with
//cout << somePhoneNumber; )
ostream &operator <<( ostream &output, const PhoneNumber &num )
{
output << "(" << num.areaCode << ")"
<< num.exchange << "-" << num.line;
return output; //enables cout << a << b << c;
}

istream &operator >>( istream &input, const PhoneNumber &num )
{
input.ignore(); //skip (
input >> setw( 4 ) >> num.areaCode; //这里报错
input.ignore( 2 ); //skip )
input >> setw( 4 ) >> num.exchange; //这里报错
input.ignore(); //skip dash (-)
input >> setw( 5 ) >> num.line; //这里报错
return input; //enables cin >> a >> b >> c;
}

int main()
{
PhoneNumber phone; //create object phone

cout << "Enter phone number in the form (123) 456-7890: \n";

//cin >> phone invokes operator >> function by
//issuing the call operator >>( cin, phone ).
cin >> phone;

//cout << phone invokes operator << function by
//issuing the call operator <<( cout, phone ).
cout << "The phone number entered was: " << phone << endl;
system("pause");
return 0;
}

提示说操作符重载未匹配,不明白
input >> setw( 4 ) >> num.areaCode; //input area code
IntelliSense: 没有与这些操作数匹配的 ">>" 运算符
操作数类型为: std::basic_istream<char, std::char_traits<char>> >> const char [4] d:\ex\25\main.cpp 37 21 25
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
discory
新手乐园 33,314 成员 41,769 内容