Just as I get one solution, I have another problem. See, I have a friend statement in my templated linked list, and I need to have it as a friend to reach my private nested Node struct.
template <typename T>
class LL;
template <typename T>
std::ofstream& operator<< (std::ofstream& output, LL<T>& obj);
template <typename T>
class LL
{
struct Node
{
T mData;
Node *mNext;
Node();
Node(T data);
};
private:
Node *mHead, *mTail;
int mCount;
public:
LL();
~LL();
bool insert(T data);
bool isExist(T data);
bool remove(T data);
void showLinkedList();
void clear();
int getCount() const;
bool isEmpty();
friend std::ofstream& operator<< <>(std::ofstream& output, LL& obj);
};
template <typename T>
std::ofstream& operator<<(std::ofstream& output, LL<T>& obj)
{
Node* tmp;
if (obj.mHead != NULL)
{
tmp = obj.mHead;
while (tmp != NULL)
{
output << tmp->mData << std::endl;
tmp = tmp->mNext;
}
}
return output;
}
Note that I need the "tmp->mData" part in the friend operator<< function definition to work with the template. Unfortunately, the only errors I get now are in the friend function definition. It doesn't understand 'Node' despite it being a friend function. I'm very confused. Hope someone can help me.