Help with overloading the << operator

Help with overloading the << operator

Post by William McLendo » Thu, 08 Mar 2001 12:24:12



Hi,
   I have this class that I'm trying to overload the << operator for,
but I get compile errors trying this.
I have some bitsets in the class that I'd like to have parameterized
with the template argument.

Can anyone show me how I'd set up the operator for this?

Thanks,
   -Will

// ============================================================
//  test.cpp
//
#include "test.h"
#include <iostream>

void main()
{
   pmark<8> X;
   pmark<16> Z;

   cout << Z << endl;
   cout << X << endl;

Quote:}

// ==============================================================
// test.h
//
#ifndef __MARK_H_
#define __MARK_H_

#ifdef __GNUC__
#include <string>
#include <algorithm>
#endif

#include <stddef.h>          // for size_t
#include <bitset>
#include <iostream.h>

template <size_t N>
class pmark
{
  public:

   bitset<N> p;
   bitset<N> s;

   pmark()  { };
   ~pmark() { };

   inline void clear()     { p.reset(); s.reset(); }
   inline void setp(int i) { p.set(i); }
   inline void sets(int i) { s.set(i); }

  protected:
  private:

Quote:};

// How would I make this operator generalized?
ostream& operator<< (ostream& s, pmark<size_t N>& mark)
{
   s << mark.p;
   return s;

Quote:}

#endif
 
 
 

Help with overloading the << operator

Post by Jon Be » Thu, 08 Mar 2001 14:43:52




Quote:>   I have this class that I'm trying to overload the << operator for,
>but I get compile errors trying this.

Tip:  it generally helps people a lot if you say what the compilation
error messages *are*, and which lines they refer to in your code.  It
saves some searching and guessing.

--

Dept. of Physics and Computer Science        Clinton, South Carolina USA

 
 
 

Help with overloading the << operator

Post by William McLendo » Thu, 08 Mar 2001 16:30:20


Ah...  I included the example code of what I'm trying to do (I'm using
g++ 2.96)

Here's what I'm getting:

mark.h:36: `operator<< (...)' must have an argument of class or
enumerated type
mark.h:36: `operator<< (...)' must take exactly two arguments
mark.h: In function `ostream &operator<< (...)':
mark.h:37: `s' undeclared (first use this function)
mark.h:37: (Each undeclared identifier is reported only once for each
function it appears in.)
mark.h:37: `mark' undeclared (first use this function)
mark.cpp: In function `int main (...)':
mark.cpp:9: warning: cannot pass objects of non-POD type `class
_IO_ostream_withassign' through `...'
mark.cpp:9: warning: cannot pass objects of non-POD type `class
pmark<16>' through `...'
mark.cpp:10: warning: cannot pass objects of non-POD type `class
_IO_ostream_withassign' through `...'
mark.cpp:10: warning: cannot pass objects of non-POD type `class
pmark<8>' through `...'

I'm looking for finding out how one would go about setting up a
templated class where I'm using the bitset<>
as my data members... I want my class to have the size parameter, but
also have the << operator overloaded.
Are there any tricks to doing this?  It's got to be possible because the
specification of bitset is
bitset<size_t N>, I can instantiate a bitset as bitset<8> foo;  and then
do cout << foo; and it'll print "00000000"

Any ideas?  =)

Thanks,
   -William

here's what I'm trying to do (my attempt at overloading this guy...)

ostream& operator<< (ostream& s, pmark<size_t N>& mark)
{
   s << mark.p << " " << mark.s;
   return s;

}



>>   I have this class that I'm trying to overload the << operator for,
>> but I get compile errors trying this.

> Tip:  it generally helps people a lot if you say what the compilation
> error messages *are*, and which lines they refer to in your code.  It
> saves some searching and guessing.

 
 
 

Help with overloading the << operator

Post by Larry Brasfiel » Thu, 08 Mar 2001 16:44:32



> Ah...  I included the example code of what I'm trying to do (I'm using
> g++ 2.96)

I'm still not seeing what mark.h is.  One thing I noticed
about your original post is that you #include both
<iostream> and <iostream.h> in the same translation
unit.  That is a formula for brain strain, I've found.

Perhaps the following code fragment, which compiles
and runs correctly (AFAICT) with another popular
near-C++ compiler, will answer your questions.

-Larry Brasfield

#include <string>
#include <algorithm>

#include <stddef.h>          // for size_t
#include <bitset>
#include <iostream>
using std::bitset;
using std::ostream;
using std::cout;
using std::endl;

template <size_t N>
class pmark
{
  public:

   bitset<N> p;
   bitset<N> s;

   pmark()  { };
   ~pmark() { };

   inline void clear()     { p.reset(); s.reset(); }
   inline void setp(int i) { p.set(i); }
   inline void sets(int i) { s.set(i); }

  protected:
  private:

Quote:};

template <size_t N>
ostream& operator<< (ostream& s, pmark<N>& mark)
{
   s << mark.p;
   return s;

Quote:}

void main()
{
   pmark<8> X;
   pmark<16> Z;

   cout << Z << endl;
   cout << X << endl;

Quote:}

 
 
 

Help with overloading the << operator

Post by William McLendo » Thu, 08 Mar 2001 17:19:18


That works great for me... thank you very much!!!

I guess I didn't notice including iostream and iostream.h -- thanks for
the heads up on that =)

-Will



>> Ah...  I included the example code of what I'm trying to do (I'm using
>> g++ 2.96)

> I'm still not seeing what mark.h is.  One thing I noticed
> about your original post is that you #include both
> <iostream> and <iostream.h> in the same translation
> unit.  That is a formula for brain strain, I've found.

> Perhaps the following code fragment, which compiles
> and runs correctly (AFAICT) with another popular
> near-C++ compiler, will answer your questions.

> -Larry Brasfield

> #include <string>
> #include <algorithm>

> #include <stddef.h>          // for size_t
> #include <bitset>
> #include <iostream>
> using std::bitset;
> using std::ostream;
> using std::cout;
> using std::endl;

> template <size_t N>
> class pmark
> {
>   public:

>    bitset<N> p;
>    bitset<N> s;

>    pmark()  { };
>    ~pmark() { };

>    inline void clear()     { p.reset(); s.reset(); }
>    inline void setp(int i) { p.set(i); }
>    inline void sets(int i) { s.set(i); }

>   protected:
>   private:

> };

> template <size_t N>
> ostream& operator<< (ostream& s, pmark<N>& mark)
> {
>    s << mark.p;
>    return s;
> }

> void main()
> {
>    pmark<8> X;
>    pmark<16> Z;

>    cout << Z << endl;
>    cout << X << endl;
> }

 
 
 

1. HELP >>>>>>>>>>>>> WINAPI <<<<<<<<<<<<<<<<<

   I need help in writting a vb program that will monitor the message
queue of another aplication that is running along with my program. Itried
to use PeekMessage api function but the help file says that it can only
peek the application's own message queue.  My program is similar in
working with SPY or the SPY++ program. Is there an api function that Ican
use. PLEEEEEEEEEZE help.

2. Default Access=Author with Delete. Why can't users edit/del docs?

3. >>>>>>>>> PROBLEM WITH MSICPB.EXE <<<<<<<<<<<<<<<<<

4. TCL for PM?

5. Wanted: Game programmer for the FPS title: "The Unholy"<<<<<<<<<<<<<<<

6. mysqld problem

7. can i overload operator< to get (a < b < c)

8. HELP >>>>>>>>>WIN API <<<<<<<<<<<

9. <<>><><><><><><><><><><>

10. Help - how to overload << operator to accept endl

11. Help with Overloading operator<<

12. Help with overloading >> and << operators