I'm attempting to write a C++11 linked list implementation, with the linked list being a template class and its node being a nested class as follows:
template <typename T>
class LinkedList {
public:
class Node;
std::shared_ptr<Node> listSearch(const T &input) const;
private:
std::shared_ptr<Node> head;
std::shared_ptr<Node> tail;
};
template <typename T>
class LinkedList<T>::Node {
private:
T data;
std::shared_ptr<Node> next;
}
I am assuming that the class Node is not a template in itself, but when LinkedList gets instantiated it creates the Node class as well.
When I attempt to define the listSearch function as follows, I get an error: "template argument for template type parameter must be a type; did you forget 'typename'?". Can someone explain what is wrong?
template <typename T>
std::shared_ptr<LinkedList<T>::Node> LinkedList<T>::listSearch(const T &input) { ... }
Edit:
Ok, so I recompiled with gcc and the error message was clearer. It wants the following:
std::shared_ptr<typename LinkedList<T>::Node> LinkedList<T>::listSearch(const T &input) const { ... }
Why is the typename necessary before LinkedList::Node? Isn't it obvious that it's a type?
look here
template <typename T>
std::shared_ptr<typename LinkedList<T>::Node> LinkedList<T>::listSearch(const T &input) { ... }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments