I have some (simple looking?) problem with the variable declaration in template friend operator overloading. The compiler gives me a message:
main.cpp|103|error: ‘ptemp’ was not declared in this scope|
Code:
template <typename K, typename I>
class Sequence
{
private:
struct Data
{
K key;
I data;
Data *pnext;
};
Data *pHead;
public:
//(...)
friend ostream &operator << <I,K> (ostream&, const Sequence<I,K>&);
};
template <typename I, typename K>
ostream &operator << (ostream& stream, const Sequence<I,K> &cop)
{
Sequence<I,K>::Data *ptemp (cop.pHead); ///here is the error (?)
stream << "-------------- PRINT BEGINS ---------------" << endl;
if (!ptemp) //there is no elements
{
stream << "The list is empty, there is nothing to print!" << endl;
stream << "-------------- PRINT ENDS ---------------" << endl << endl;
return stream;
};
}
Compilers gives message that there is no declarated "ptemp" when I do a declaration. The same is when I erase initialization of ptemp. I can't understand what is wrong in this declaration. I will be grateful for any suggestions.
Try the following