64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <iomanip>
class PhoneNumber{
friend ostream &operator<<(ostream&, const PhoneNumber&);
friend istream &operator>>(istream &, PhoneNumber &);
private:
char areaCode[4];
char exchange[4];
char line[5];
};
ostream &operator<<(ostream &output,const PhoneNumber & num)
{
output << "(" << num.areaCode <<") "
<< num.exchange << "-" << num.line;
return output;
}
istream &operator>>(istream &input, PhoneNumber & num)
{
input.ignore();
input >> setw(4) >>num.areaCode;
input.ignore(2);
input >> setw(4) >> num.exchange;
input.ignore();
input >> setw(5) >> num.line;
return input;
}
int main()
{
PhoneNumber phone;
cout << "Enter a phone number in the form of (123) 456-789:\n";
cin >> phone;
cout << "The phone number entered was: " << phone <<endl;
return 0;
}
1>fig08_03.cpp
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C2143: syntax error : missing ';' before '&'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C2433: 'ostream' : 'friend' not permitted on data declarations
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C2061: syntax error : identifier 'ostream'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : error C2805: binary 'operator <<' has too few parameters
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C2143: syntax error : missing ';' before '&'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C2433: 'istream' : 'friend' not permitted on data declarations
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C2061: syntax error : identifier 'istream'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : error C2805: binary 'operator >>' has too few parameters
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(12) : error C2143: syntax error : missing ';' before '&'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(12) : error C2086: 'int ostream' : redefinition
1> d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(5) : see declaration of 'ostream'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(12) : error C2065: 'output' : undeclared identifier
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(12) : error C2059: syntax error : 'const'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(13) : error C2143: syntax error : missing ';' before '{'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(13) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C2143: syntax error : missing ';' before '&'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C2086: 'int istream' : redefinition
1> d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(6) : see declaration of 'istream'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C2065: 'input' : undeclared identifier
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C2065: 'num' : undeclared identifier
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C2275: 'PhoneNumber' : illegal use of this type as an expression
1> d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(4) : see declaration of 'PhoneNumber'
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\myc++study\fig08_03\fig08_03\fig08_03.cpp(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://d:\myC++Study\fig08_03\fig08_03\Debug\BuildLog.htm"
1>fig08_03 - 27 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
using namespace std;
#include <iostream>
#include <iomanip>
using namespace std;
class PhoneNumber{
friend ostream &operator<<(ostream&, const PhoneNumber&);
friend istream &operator>>(istream &, PhoneNumber &);
private:
char areaCode[4];
char exchange[4];
char line[5];
};
ostream &operator<<(ostream &output,const PhoneNumber & num)
{
output << "(" << num.areaCode <<") "
<< num.exchange << "-" << num.line;
return output;
}
istream &operator>>(istream &input, PhoneNumber & num)
{
input.ignore();
input >> setw(4) >>num.areaCode;
input.ignore(2);
input >> setw(4) >> num.exchange;
input.ignore();
input >> setw(5) >> num.line;
return input;
}
int main()
{
PhoneNumber phone;
cout << "Enter a phone number in the form of (123) 456-789:\n";
cin >> phone;
cout << "The phone number entered was: " << phone <<endl;
return 0;
}
//VS2010 通过