Victor Bazarov wrote:
It doesn't have to be const. The error messages you received were most
like due to the way you used your operator -- the object which you wanted
to output _was_ in fact a const object, probably. But as you see, I am
guessing here -- you didn't follow the recommendations of FAQ 5.8.
OK, here is the code and error message:
************************************************** ***********************
#include <iostream>
#include <iomanip>
using namespace std;
class Base
{
public:
~Base(){}
Base(int val, int num) { }
friend ostream& operator<< ( ostream&, Base& );
};
ostream& operator<< ( ostream &os, Base& obj )
{
return os << "hi";
}
int main()
{
int u5 = 100;
cout << setw(0) << Base(u5,0) << endl;
return 0;
}
************************************************** ***********************
g++ o.cpp -o o
o.cpp: In function `int main()':
o.cpp:22: error: no match for 'operator<<' in 'std::operator<< [with
_CharT = char, _Traits =
std::char_traits<char>](((std::basic_ostream<char,
std::char_traits<char&)(&std::cout)), std::setw(0)) << Base(u5, 0)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63:
note: candidates are: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74:
note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
<<< The list of candidates is quite long >>>
Thanks
Wick