I've read that this is a very common problem in this kind of situation. I have Google'd, referred to class notes, and just done lots of research, but can't seem to figure out how to get this to work. My current code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <string.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
template <class T>
class Book
{
private:
struct Node
{
T *data;
Node *next;
};
Node *head;
public:
Book<T>();
... getters and setters...
friend ostream& operator<<(ostream &outStream, const Book<T> &other)
{
Node *current = head;
while(current != NULL)
{
outStream << *(current->data);
current = current->next;
}
return outStream;
}
~Book<T>();
};
|
In this situation, I get:
error C2597: illegal reference to non-static member 'Book<T>::head'
If I implement the function outside of the .h, and instead just have:
friend ostream& operator<<(ostream &outStream, const Book<T> &other);
in there, then I will also get errors.
SO far I've used every strategy from:
http://www.parashift.com/c++-faq/template-friends.html
And every single top result from Google.
Help would be appreciated