rdf
asked on
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.
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;
}
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rdf