简体   繁体   English

C++模板类'内部class访问

[英]C++ template class' inner class access

I have a class like this:我有一个这样的 class:

#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;
    }
};

I also have a function template somewhere else below this class in the same file that tries to access Node and looks like this with two compiler errors:我还有一个 function 模板在这个 class 下面的其他地方,在同一个文件中,它试图访问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

After sometime I tried this, again with two compiler:一段时间后,我再次尝试了两个编译器:

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

After sometime again, I tried the below which compiled fine.又过了一段时间,我尝试了下面编译好的。

template <typename T>
int getSize(typename LL<T>::template Node<T>* node)
{
    if (node)
    {
        return 1 + getSize(node->m_next);
    }
    return 0;
}

Now, when I tried to call this function from my driver function, I got compiler errors again:现在,当我试图从我的驱动程序 function 调用这个 function 时,我再次遇到编译器错误:

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'
}

I tried everything that I possible could and couldn't sort this problem out.我尽了一切可能但无法解决这个问题。 Could someone please explain me what is going on?有人可以向我解释发生了什么事吗? Note: All codes that I've mentioned here are in the same file.注意:我在这里提到的所有代码都在同一个文件中。

getSize(ll.head()) fails because of non-deduced context ; getSize(ll.head())由于未推导上下文而失败; template parameter T can't be deduced automatically.无法自动推导模板参数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: 1) 使用限定标识指定的类型的嵌套名称说明符(scope 解析运算符:: 左侧的所有内容):

The declaration should be声明应该是

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

And since Node is not a template you don't need to use template keyword.由于Node不是模板,因此您不需要使用template关键字。

LIVE居住


See Where and why do I have to put the “template” and “typename” keywords?请参阅我必须在何处以及为什么必须放置“模板”和“类型名称”关键字? about why use the keyword typename and template .关于为什么使用关键字typenametemplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM