Overloaded leftshift operator special case
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.
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.
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 DobbsYes, you can. The problem, AFAICT, is that <iostream> is not included and that is where endl is declared.
I don''t think you can use endl that way.
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...
#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]
[ 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]
Yes, what Mr. Fruny () said. Just to add clarity, you do this:
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
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 ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement