This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: operator<< overload question
Allen Wayne Best <gcc@atoka-software.com> writes:
> good idea; here is the complete file:
Well, this example doesn't compile because you commented out the
declaration of the relevant operator>>:
> // friend ostream& operator<<( ostream& o , const Sensor& s ) ;
When I uncomment that line, the file compiles.
When I try to link it, I get
foo1.o(.text+0x6b): undefined reference to `Measure::operator<<(std::ostream&, Measure::Sensor const&)'
That's because of this line:
> ostream& operator<<( ostream& o , const Sensor& s)
That defines operator<<, not Measure::operator<<. Your friend
declaration was inside namespace Measure.
When I change the function definition to:
ostream& Measure::operator<<( ostream& o , const Sensor& s)
then the file compiles and links.
Alternatively, if I add these lines at the top:
namespace Measure { class Sensor; }
extern ostream& operator<<( ostream& o , const Measure::Sensor& s );
and then change the friend declaration to
friend ostream& ::operator<<( ostream& o , const Sensor& s ) ;
then the file compiles and links.
Ian