Why can't I overload of iostream operator?

This is a discussion on Why can't I overload of iostream operator? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> using namespace std; class PhoneNumber { friend ostream &operator>>(ostream &, const PhoneNumber &); friend istream ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    Why can't I overload of iostream operator?

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    It should be:
    Code:
    friend ostream &operator<<(ostream &, const PhoneNumber &);
    friend istream &operator>>(istream &, PhoneNumber &);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Thanks. I have corrected it. But the wrong message is still appearing.
    Code:
    mathsniper:~# g++ -o overload overload.cpp
    overload.cpp: In function ‘std::istream& operator>>(std::istream&, const PhoneNumber&)’:
    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> >&)(+ input)), std::setw(4)) >> num->PhoneNumber::areaCode’
    /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/istream:734: note: candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    ...........................

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Thanks. I have corrected it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Troublesome operator overload
    By ruthgrin in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2006, 04:16 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21