________________________________________________________________Bag.H

#ifndef BAG_H
#define BAG_H

#include <iostream>
using namespace std;


template <class T>
class Bag       // stands for a container class, like a list, set, etc.
{
   private:
        T t1;
        T t2;

   friend ostream& operator<< <> (ostream& ,const Bag<T>& );
};


template <class T>
ostream& operator<< (ostream& o,const Bag<T>& b)
{
        o << b.t1 << "," << b.t2;
        return o;
}

#endif

________________________________________________________Nut.H

#ifndef NUT_H
#define NUT_H

#include <iostream>
using namespace std;

class Nut
{
   private:
        int k;
   friend ostream& operator<< (ostream&,const Nut&);
};

ostream& operator<< (ostream& o,const Nut& n)
{       o << n.k;
        return o;
}

#endif

_________________________________________________________BagOfNuts.H

#ifndef BAG_OF_NUTS
#define BAG_OF_NUTS

#include "Bag.H"
#include "Nut.H"
#include <iostream>
using namespace std;


class BagOfNuts: public Bag<Nut>
{
        
        friend ostream& operator<< <> (ostream& ,const Bag<Nut>&);
};

#endif


________________________________________________________useBagOfNuts.c

#include "BagOfNuts.H"

#include <iostream>
using namespace std;

int main()
{
        BagOfNuts b;
        cout << b << endl;

        return 0;
}

_________________________________________________Compiler errors g++ 2.96

[ringland@dhcppc1 BH2.1_2002]$ g++ -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/specs
gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)


[ringland@dhcppc1 BH2.1_2002]$ g++ useBagOfNuts.c 
Bag.H: In function `ostream &operator<< (ostream &, const Bag<T> &) 
[with T = Nut]':
useBagOfNuts.c:10:   instantiated from here
Bag.H:16: `Nut Bag<Nut>::t2' is private
Bag.H:25: within this context
Bag.H:15: `Nut Bag<Nut>::t1' is private
Bag.H:25: within this context
[ringland@dhcppc1 BH2.1_2002]$ 


________________________________________________Compiler errors g++ 3.2

[ringland@orange BH2.1_2002]$ g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)


[ringland@orange BH2.1_2002]$ g++ useBagOfNuts.c
useBagOfNuts.c: In function `int main()':
useBagOfNuts.c:10: ambiguous overload for `std::ostream& << BagOfNuts&'
   operator
BagOfNuts.H:13: candidates are: std::ostream& operator<<(std::ostream&, const
   Bag<T>&) [with T = Nut]
Bag.H:24:                 std::ostream& operator<<(std::ostream&, const
   Bag<T>&) [with T = Nut]
BagOfNuts.H:13:                 std::ostream& operator<<(std::ostream&, const
   Bag<T>&) [with T = Nut]
[ringland@orange BH2.1_2002]$
