Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling overloaded operator

Posted on 1999-06-29
2
Medium Priority
?
261 Views
Last Modified: 2010-04-02
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.
Thanks

******stack.h***********

   #ifndef STACK_T
   #define STACK_T

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

   #endif


 *****.cpp****************

 ostream& operator<<(ostream& os, const StackType& S)
                                           
   {
      os << '<';
      for(int i = 0; i<= S.Top; ++i)
      {  
         os<<S.data[i];
         cout<<S<<endl;
      }
     
      os << '>';
      return os;
   }  










       

                       
















                       



0
Comment
Question by:rdf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 80 total points
ID: 1198754
You use the << operator with a ostream on the left and a StackType on the right.
Like

cout << SomeStack;

you could also do

operator <<(cout,SomeStack);

but that is pretty ugly.  Let me know if you have any questions.
0
 

Author Comment

by:rdf
ID: 1198755
thanks nietod
rdf
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question