[prog] Overloading operator<<

Kathryn Andersen kat_lists at katspace.com
Tue Jun 10 19:43:53 EST 2003


On Tue, Jun 10, 2003 at 07:00:02PM +1000, Sue Stones wrote:
> Compiler errors have been added below the repective code.

> 
> On Tue, 10 Jun 2003 05:48 pm, Sue Stones wrote:
> > I am trying to write an function to define << for the class widgit, but I
> > can't get it to compile.  What am I doing wrong.  Below are a couple of the
> > options that I have tried, I have copied the class definition and the
> > problem function only!
> >
> > sue
> >
> > ------attempt1-----------
> >
> > #include <iostream>
> > #include <strings.h>
> > using std::cout;
> > using std::endl;
> >
> > class Widgit {
> > private:
> >         int id_num;
> >         char *description;
> > public:
> >         Widgit() {} // default constructor
> >         Widgit(int id, char * desc ); // init constructor
> >         Widgit(const Widgit& w); // copy constructor
> >         Widgit& operator= (const Widgit& w); // define assignment for
> > Wigits ostream& operator<< (ostream&, const Widgit&);
> > 					 // define ostream << for Wigits
> > };//widget
> >
> > ostream& operator<< (ostream& out, const Widgit& w){
> >         out << w.id_num << ": "
> >                 << w.description << endl;
> >         return out;
> > }  // define ostream << for Wigits
> 
> 
> g++     Widgit.C   -o Widgit
> Widgit.C:15: syntax error before `&' token
> Widgit.C:39: syntax error before `&' token
> make: *** [Widgit] Error 1
> [
> 
> g++     Widgit.C   -o Widgit
> Widgit.C:7: ISO C++ forbids declaration of `ostream' with no type
> Widgit.C:7: `ostream' is neither function nor member function; cannot be 
> declared friend
> Widgit.C:7: parse error before `&' token
> Widgit.C:39: syntax error before `&' token
> make: *** [Widgit] Error 1

Both errors look like the same error: it isn't recognising 'ostream' as
a defined class.  Should it be std::ostream instead?

-- 
 _--_|\	    | Kathryn Andersen	<http://www.katspace.com>
/      \    | 
\_.--.*/    | GenFicCrit mailing list <http://www.katspace.com/gen_fic_crit/>
      v	    | 
------------| Melbourne -> Victoria -> Australia -> Southern Hemisphere
Maranatha!  |	-> Earth -> Sol -> Milky Way Galaxy -> Universe


More information about the Programming mailing list