-1

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.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76

1 Answers1

0

Being a friend, and access control in general, never affects name lookup. (This is sometimes unhelpful, but the orthogonality is easy to reason about and guarantees that changing a member’s access doesn’t change the meaning of a program (except in certain SFINAE cases), just possibly make it not compile.) So just as being private doesn’t prevent finding a name, being a friend doesn’t help find it. Use

typename LL<T>::Node *tmp;
Davis Herring
  • 36,443
  • 4
  • 48
  • 76