Thread: Overloaded operator "<<"

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Overloaded operator "<<"

    Code:
    #include <fstream>
    using std ofstream;
    .
    .
    .
    ofstream &operator<<(ofstream& stream, const Date& d){
    	stream << d.getMonth() << '|'  //error found here
    	            << d.getDay() << '|'
    	            << d.getYear() << '|';
    
    	return stream;
    }
    The codes above gave me this error.
    e:\jdocument\csci\231\no1\date.cpp(132) : error C2666: '<<' : 13 overloads have similar conversions
    What's weird is when I change all "ofstream" to "ostream" and change to appropriate "include" and "using", it compiles fine.
    So, any idea..???

  2. #2
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Which compiler do you use?
    And how is defined the Date class?

    Because, if I try to compile the code below:
    Code:
    #include <fstream>
    using std::ofstream;
    
    struct Date {
        int getMonth() const;
        int getDay() const;
        int getYear() const;
    };
    
    ofstream &operator <<(ofstream& stream, const Date& d)
    {
    	stream << d.getMonth() << '|'  //error found here
    	            << d.getDay() << '|'
    	            << d.getYear() << '|';
    
    	return stream;
    }
    I don't get any error when compiling. But, if I try to use MSVC6, it displays many errors but there are in the STL headers so it's not your fault anyway...

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I suspect that the error has got something to do with other parts of my codes, so I guess I have to figure this out myself. Thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  4. Cannot resolve overloaded function
    By Mithoric in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2003, 03:40 AM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM