5

I'm actually implementing a doubly-linked list in C++.

Here's a MWE of sorts:

namespace mynamespace {

template <typename T>
class List {
public:
    List();

    void prepend(T);
    void append(T);
    void remove(T);

private:
    struct Node {
        T value_;
        Node * prev_;
        Node * next_;
    };

private:
    Node * find(T); // <-- THIS IS MY PROBLEM

private:
    Node * head_;
    Node * tail_;
};

}

The reason I'd like to create that function is because I figured it'd be handy if I could traverse the list with a function like that until I find a given element (I'll need to do the same with the remove() function anyways)

But how do I define that function outside the class definition?

Since Node is a private member of the List class, this is not working:

template <typename T>
Node * List<T>::find(T val)
{
    // stuff
}

I suppose defining the function inside the class definition would work, because Node makes sense there... Would that be the proper way? Even if so, I suppose there must be a way to define the function the way I'm trying to...

3
  • Just like you have to do List<T>::find to tell the compiler where find is you need to do the same thing to Node. Apr 5, 2018 at 17:55
  • @Justin thanks, that works! I was missing the typename (I tried what you and @NathanOliver initally said, but I thought that was wrong too, because w/o the typename, I got errors). Would you please add this as an answer so that I can mark it as a solution? Also, would you explain why I need the typename?
    – bp99
    Apr 5, 2018 at 17:59
  • 2
    Note that in templates, I generally tend to use const T& arguments rather than T, so in case T is something like std::string there won't be an unnecessary copy. (A possible exception would be "insert" type operations like your prepend or append where you can use a pass-by-value and then move the parameter into the container, in order to avoid having to write separate copy prepend(const T&) and move prepend(T&&) versions of the method.) Apr 5, 2018 at 18:19

1 Answer 1

8

Since Node is a private member of the List class, this is not working:

Actually, that's not correct. It's not failing because Node is private, but because Node is nested inside of List. The name of the Node class isn't Node, it's List<T>::Node. However, since Node depends on T, you have to write typename List<T>::Node, otherwise the compiler assumes that List<T>::Node is a value rather than a type. See this question for more information.

In short, replace this:

template <typename T>
Node * List<T>::find(T val)

With this:

template <typename T>
typename List<T>::Node * List<T>::find(T val)

Alternatively, as StoryTeller noted, if you are in the context of the List<T> class, you can just use Node. You can get in this context by using a trailing return type:

template <typename T>
auto List<T>::find(T val) -> Node *
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.