Code:
#include <iostream>
#include <iomanip>
using namespace std;
class PhoneNumber {
friend ostream &operator>>(ostream &, const PhoneNumber &);
friend istream &operator<<(istream &, const 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, const 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 phone number in the form (123) 456-7890:\n";
cin >> phone;
cout << "The phone number entered was: " << phone << endl;
return 0;
}
many many wrong message was appearing......
Code:
mathsniper:~# g++ -o overload overload.cpp
overload.cpp: In function ‘std::ostream& operator<<(std::ostream&, const PhoneNumber&)’:
overload.cpp:10: error: ‘char PhoneNumber::areaCode [4]’ is private
overload.cpp:17: error: within this context
overload.cpp:11: error: ‘char PhoneNumber::exchange [4]’ is private
overload.cpp:18: error: within this context
overload.cpp:12: error: ‘char PhoneNumber::line [5]’ is private
overload.cpp:18: error: within this context
overload.cpp: In function ‘std::istream& operator>>(std::istream&, const PhoneNumber&)’:
overload.cpp:10: error: ‘char PhoneNumber::areaCode [4]’ is private
overload.cpp:25: error: within this context
overload.cpp:25: error: ambiguous overload for ‘operator>>’ in ‘std::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> ...........................................
Could anyone tell me was wrong with the code?