Overloaded leftshift operator special case

Started by
5 comments, last by Geronimo2000 20 years, 9 months ago
I have a question for a somewhat hardcore developer. Why do I get the following compiler error: error C2679: binary ''<<'' : no operator defined which takes a right-hand operand of type ''class A'' (or there is no acceptable conversion) for this code: #include <fstream> using namespace std; class A { }; ofstream &operator<<(ofstream &stream, const A &rhs) { return stream; } void main() { ofstream stream; A var; stream << endl << var; // error! stream << var; // no error! } Please help! I''ve seen posts on the web for this problem in most languages going back several years. All the posted solutions were to stop using the .h includes (like using iostream instead of iostream.h), but the code I posted is all the code in the file and as you can see I''m using the newer include files! I''ve run out of ideas - thanks for any help.
Advertisement
I don''t think you can use endl that way. You could do:

stream << ''\n'' << var;

I''m pretty sure using endl doesn''t return a stream in the way your overloaded operator does. It probably returns void.
quote:Original post by Dobbs
I don''t think you can use endl that way.
Yes, you can. The problem, AFAICT, is that <iostream> is not included and that is where endl is declared.
Thanks for your help, but I''m afraid the solution hasn''t been uncovered yet. Check out some newer code which won''t compile:

#include <fstream>
#include <iostream>
using namespace std;

class A
{
};

ofstream &operator<<(ofstream &stream, const A &rhs)
{
return stream;
}

void main()
{
ofstream stream;
A var;
stream << " " << var; // error!
stream << endl << var; // error!
stream << var << var; // no error
stream << var; // no error
}

I hope somebody out there can save me! I think this is a tough one...
iostream operations take and return ostream, not ofstream, your own overloads must be designed the same way. Otherwise, you'll receive a 'base class' object when a 'derived class' object is expected ... and that cannot work.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on July 2, 2003 7:40:15 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Also, just so you know, iostream is included in fstream.


doh, nuts. Mmmm... donuts
My website
Yes, what Mr. Fruny () said. Just to add clarity, you do this:

class CTest
{
...
};

std::ostream& operator<<( std::ostream& os, const CTest& rhs );

std::ostream& operator<<( std::ostream& os, const CTest& rhs )
{
...

return os;
}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement