I have to implement a template class called Stack
for a class assignment. Stack has an overloaded stream insertion operator.
Here is a related excerpt from the Stack.h file:
...
#include <iostream>
using namespace std;
template<class T>
ostream& operator<<(ostream&,Stack<T>&);
template<class T>
class Stack
{
public:
friend ostream& operator<< <T>(ostream&,Stack<T>&);
...
};
#include "Stack.cpp"
...
I cannot change Stack.h since it was given as is. the related excerpt from Stack.cpp is:
template <class T>
ostream& operator<< (ostream& out, Stack<T>& stack)
{
Stack<T>::Node* current = stack.top;
out << "[";
while (current)
{
out << current->element;
current = current->next;
if (current)
out << ",";
}
out << "]";
return out;
}
...
This compiles and works fine in Visual Studio, however, when compiled with g++, it gives the following errors:
Stack.cpp:4: syntax error before '&'
Stack.cpp:4: 'ostream' was not declared in this scope
Stack.cpp:4: 'out' was not declare din this scope
Stack.cpp:4: 'Stack' was not declared in this scope
Stack.cpp:4: 'T' was not declared in this scope
Stack.cpp:4: 'stack' was not declared in this scope
Stack.cpp:5: declaration of 'operator <<' as non-function
why is this? What can be done to fix it?
EDIT: I added that I have already included iostream and provided the namespace.