c++ - C++模板类的内部类访问

标签 c++ templates function-templates

我有一个这样的课:

#include <iostream>

template <class T>
class LL
{
    using size_t = unsigned int;

    class Node
    {
        T m_data;
        Node* m_next;

        Node(const T& data) :m_data{ data }, m_next{ nullptr }{}

        friend std::ostream& operator<<(std::ostream& out, const Node& node)
        {
            out << node.m_data;
            return out;
        }

        friend std::ostream& operator<<(std::ostream& out, const LL& ll);

        friend class LL;
    };

    Node* m_first{ nullptr };
    size_t m_size{ 0 };

    Node* newNode(const T& data)
    {
        return new Node{ data };
    }

public:
    void push(const T& data)
    {
        Node* temp = newNode(data);
        temp->m_next = m_first;
        m_first = temp;
        ++m_size;
    }

    Node* head()
    {
        return m_first;
    }

    size_t size() const
    {
        return m_size;
    }

    ~LL()
    {
        if (m_first)
        {
            Node* trav = m_first->m_next;
            Node* foll = m_first;
            while (trav)
            {
                delete foll;
                foll = trav;
                trav = trav->m_next;
            }
            delete foll;
        }
    }

    friend std::ostream& operator<<(std::ostream& out, const LL& ll)
    {
        Node* trav = ll.m_first;
        while (trav)
        {
            out << *trav << ' ';
            trav = trav->m_next;
        }
        return out;
    }
};

我在同一文件的此类下的其他位置也有一个函数模板,该文件尝试访问Node,看起来像这样,但有两个编译器错误:
template <typename T>
int getSize(LL<T>::Node* node) //C2065: node is undeclared, C3861: node is not found
{
    if (node)
    {
        return 1 + getSize(node->m_next);
    }
    return 0;
} //does not compile

一段时间后,我再次尝试使用两个编译器:
template <typename T>
int getSize(LL<T>::Node<T>* node) //C2065 like before, C7510: use of dependent template name must be prefixed with 'template'
{
    if (node)
    {
        return 1 + getSize(node->m_next);
    }
    return 0;
} //does not compile

再过一段时间后,我尝试了以下编译良好的方法。
template <typename T>
int getSize(typename LL<T>::template Node<T>* node)
{
    if (node)
    {
        return 1 + getSize(node->m_next);
    }
    return 0;
}

现在,当我尝试从驱动程序函数调用此函数时,再次出现编译器错误:
int main()
{
    LL<int> ll;
    std::cout << getSize(ll.head()); //E0304, C2672 and C2783

    //E0304: no instance of the function template "getSize" matches the argument list
    //C2672: no matching overload function found
    //C2783: could not deduce template argument for 'T'
}

我尝试了所有可能且无法解决此问题的方法。有人可以解释一下发生了什么吗?
注意:我在这里提到的所有代码都在同一个文件中。

最佳答案

getSize(ll.head())由于non-deduced context而失败;无法自动推导模板参数T

If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

  • 1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:


声明应为
template <typename T>
int getSize(typename LL<T>::Node* node) // using class instead of typename works for OP (MSVC)
{
    //some code
}

而且由于Node不是模板,因此不需要使用template关键字。

LIVE

有关为什么使用关键字typenametemplate的信息,请参见Where and why do I have to put the “template” and “typename” keywords?

关于c++ - C++模板类的内部类访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61865076/

相关文章:

c++ - 重新排列已排序的数组

c++ - 如何避免通过参数依赖查找显式专门化模板化函数

c++ - Herb Sutter 的 C++Con 2014 演讲中的完美转发器

c++ - 在构造函数的可变参数中使用其他模板化类执行模板化类的初始化

c++ - 在没有原始循环的情况下累积相等的范围

c++ - 用作模板的类中的静态常量变量

c++ - 如果模板是整数类型,则仅对某些模板规范启用功能

html - 日期标题重叠帖子标题

C++ 部分模板特化语法

c++ - 使用运行时常量实例化的函数模板