C++ Nested class in class template declaration

JamesLens

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?

Xiaotian Pei

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Member declaration not found" in a nested template class in C++

From Dev

Nested class declaration: template vs non-template outer class

From Dev

Forward declaration of template class nested inside template class

From Dev

Nested class declaration: template vs non-template outer class

From Dev

Forward declaration of a template class c++

From Dev

Move method definition for template nested class outside declaration

From Dev

Template class method declaration failed

From Dev

Conditional reference declaration in template class

From Dev

C++ invoke nested template class destructor

From Dev

"class VARIABLE" in declaration of variable for template class?

From Dev

"class VARIABLE" in declaration of variable for template class?

From Dev

c++ template using declaration from base class

From Dev

Nested class template specialization

From Dev

Nested class as a template parameter

From Dev

variadic template in nested class

From Dev

Resolve template with a nested class

From Dev

Nested class as a template parameter of parent class in C++

From Dev

Nested class as a template parameter of parent class in C++

From Dev

Explicit specialization of nested class template in class template

From Dev

Why is this declaration of a function in template class invalid?

From Dev

Forward class declaration with template using typename

From Dev

Forward declaration of (sdk) class containing template

From Dev

How to achieve forward declaration of template class

From Dev

How to separate definition and declaration of child template class

From Dev

why Specialized template class need forward declaration?

From Dev

using declaration of a specialized variadic template class

From Dev

Template Friend Class: Forward Declaration or...?

From Dev

Declaration of dependent type as function parameter in template class

From Dev

std::map type signature (declaration) of template class

Related Related

  1. 1

    "Member declaration not found" in a nested template class in C++

  2. 2

    Nested class declaration: template vs non-template outer class

  3. 3

    Forward declaration of template class nested inside template class

  4. 4

    Nested class declaration: template vs non-template outer class

  5. 5

    Forward declaration of a template class c++

  6. 6

    Move method definition for template nested class outside declaration

  7. 7

    Template class method declaration failed

  8. 8

    Conditional reference declaration in template class

  9. 9

    C++ invoke nested template class destructor

  10. 10

    "class VARIABLE" in declaration of variable for template class?

  11. 11

    "class VARIABLE" in declaration of variable for template class?

  12. 12

    c++ template using declaration from base class

  13. 13

    Nested class template specialization

  14. 14

    Nested class as a template parameter

  15. 15

    variadic template in nested class

  16. 16

    Resolve template with a nested class

  17. 17

    Nested class as a template parameter of parent class in C++

  18. 18

    Nested class as a template parameter of parent class in C++

  19. 19

    Explicit specialization of nested class template in class template

  20. 20

    Why is this declaration of a function in template class invalid?

  21. 21

    Forward class declaration with template using typename

  22. 22

    Forward declaration of (sdk) class containing template

  23. 23

    How to achieve forward declaration of template class

  24. 24

    How to separate definition and declaration of child template class

  25. 25

    why Specialized template class need forward declaration?

  26. 26

    using declaration of a specialized variadic template class

  27. 27

    Template Friend Class: Forward Declaration or...?

  28. 28

    Declaration of dependent type as function parameter in template class

  29. 29

    std::map type signature (declaration) of template class

HotTag

Archive