jermyn
asked on
Friend Functions
How do you declare friend functions that are not part of the class . 2) How do you declare a friend that has an entire class, example friend class Stack<SE>.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Welcome back, alexo! ronslow is back too.
Not really back, just a brief visit. Too much things to do, too little time.
I'm always reachable by email though.
I'm always reachable by email though.
ASKER
I'm still kinda lost, I have a declaration:
template < class SE >
class StackNode // Facilitator class for the Stack class
{
private:
// Constructor
StackNode ( const SE &elem, StackNode *nextPtr );
// Data members
SE element; // Stack element
StackNode *next; // Pointer to the next element
friend class Stack<SE>;
};
template < class SE >
class Stack
{
public:
// Constructor
Stack ( int ignored = 0 );
// Destructor
~Stack ();
// Stack manipulation operations
void push ( const SE &newElement ); // Push element
SE pop (); // Pop element
void clear (); // Clear stack
// Stack status operations
int empty () const; // Stack is empty
int full () const; // Stack is full
// Output the stack structure -- used in testing/debugging
void showStructure () const;
// In-lab operation
Stack ( const Stack &valueStack ); // Copy constructor
private:
// Data member
StackNode<SE> *top; // Pointer to the top element
};
and these are som of the error messages I'm getting:
error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'
error C2989: 'Stack' : template class has already been defined as a non-template class
error C2059: syntax error : '<'
error C2588: '::~Stack' : illegal global destructor
my question is how do I declare Stack?
template < class SE >
class StackNode // Facilitator class for the Stack class
{
private:
// Constructor
StackNode ( const SE &elem, StackNode *nextPtr );
// Data members
SE element; // Stack element
StackNode *next; // Pointer to the next element
friend class Stack<SE>;
};
template < class SE >
class Stack
{
public:
// Constructor
Stack ( int ignored = 0 );
// Destructor
~Stack ();
// Stack manipulation operations
void push ( const SE &newElement ); // Push element
SE pop (); // Pop element
void clear (); // Clear stack
// Stack status operations
int empty () const; // Stack is empty
int full () const; // Stack is full
// Output the stack structure -- used in testing/debugging
void showStructure () const;
// In-lab operation
Stack ( const Stack &valueStack ); // Copy constructor
private:
// Data member
StackNode<SE> *top; // Pointer to the top element
};
and these are som of the error messages I'm getting:
error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'
error C2989: 'Stack' : template class has already been defined as a non-template class
error C2059: syntax error : '<'
error C2588: '::~Stack' : illegal global destructor
my question is how do I declare Stack?
How do you define the member functions?
template <class SE>
Stack<SE>::Stack(int ignored)
{
// Do something...
}
template <class SE>
Stack<SE>::~Stack()
{
// Do something...
}
Etc...
template <class SE>
Stack<SE>::Stack(int ignored)
{
// Do something...
}
template <class SE>
Stack<SE>::~Stack()
{
// Do something...
}
Etc...