重载operator<<的问题,请进!
template<typename T>
class Stack{
friend ostream&
operator<<(ostream&, const Stack<T>&);
...;
};
template<typename T>
Stack<T>::ostream&
operator<<(ostream& out, const Stack<T>& s)
{
//output the stack s from top to bottom
//output stack size
out<<"The stack has"<<(s.top+1)
<<"element(s)"<<endl;
//output stack elements
out<<"The element(s) frome top to bottom are"<<endl;
for(int i=s.top;i>=0;i--)
out<<s.stack[i]<<' ';
out<<endl;
return out;
}
$g++ -o stack stack.cpp
In file included from stack.cpp:2:
/home/freeman/cp/stack/Stack.h:13: warning: friend declaration `std::ostream&
operator<<(std::ostream&, const Stack<T>&)' declares a non-template function
/home/freeman/cp/stack/Stack.h:13: warning: (if this is not what you intended,
make sure the function template has already been declared and add <> after
the function name here) -Wno-non-template-friend disables this warning
/home/freeman/cp/stack/Stack.h:15: warning: friend declaration `std::istream&
operator>>(std::istream&, Stack<T>&)' declares a non-template function
/home/freeman/cp/stack/Stack.h:32: error: syntax error before `&' token
/home/freeman/cp/stack/Stack.h:32: confused by earlier errors, bailing out