calling overloaded operator

Hi all, I have a operator (cout extraction)) which I think I have overloaded correctly.

My question is how do I call this function from the driver if it is not a member method? In the driver, do I :

S.operator<<(); ?
S.<<(); ?

Shouldn't I be able to just print out the stack, S, without using a print() function, but instead via this overloaded operator?????
Neither of these seems to work.


   #ifndef STACK_T
   #define STACK_T

   typedef int ItemType;
   class StackType
         StackType(int Size);
         int IsEmpty();
         int Push(const ItemType& Item);
         int Pop(ItemType& Item);
         //void print();
         void IsClear();
         friend class ostream& operator<<(ostream& os, const StackType& S);
         int Size;
         int Top;
         ItemType* data;



 ostream& operator<<(ostream& os, const StackType& S)
      os << '<';
      for(int i = 0; i<= S.Top; ++i)
      os << '>';
      return os;




Who is Participating?
nietodConnect With a Mentor Commented:
You use the << operator with a ostream on the left and a StackType on the right.

cout << SomeStack;

you could also do

operator <<(cout,SomeStack);

but that is pretty ugly.  Let me know if you have any questions.
rdfAuthor Commented:
thanks nietod
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.