关于操作符重载的问题
#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
...全文
相关推荐
C++ new、delete(new[]、delete[])操作符重载需要注意的问题
主要介绍了C++ new、delete(new[]、delete[])操作符重载需要注意的问题,本文同时讲解了兼容默认的 new、delete 的错误处理方式、多态的问题等内容,需要的朋友可以参考下
实现操作符重载 实现操作符重载
实现操作符重载实现操作符重载实现操作符重载实现操作符重载实现操作符重载
操作符重载详解
c++操作符重载详解,有详细的代码~已经调试好了可以运行的~
操作符重载
操作符重载
C++重载++与--操作符[可运行源码]
本文详细介绍了C++中重载自增(++)和自减(--)操作符的原理与实现方法。文章首先对比了C与C++中前置和后置++、--操作符的区别,指出在C++中前置操作符可以作为左值。接着,通过图解和代码示例,解释了如何通过重载操作符来实现前置和后置的不同行为,其中后置操作符通过增加一个无用的int类型参数来区分。文章还总结了前置和后置操作符的效率差异,指出后置操作符由于需要生成临时对象而效率较低。最后,通过一个知识点习题巩固了所学内容,并鼓励读者留言讨论。


新手乐园
