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

我有这样的课:

template <class T>
class LL
{
        class Node
        {
            //some code
        };

 public:
        Node* head()
        {
            return m_first;
        }
};

I also have a function template somewhere else that tries to access Node and looks like this with two compiler errors:

template <typename T>
int getSize(LL<T>::Node* node) //C2065: node is undeclared, C3861: node is not found
{
    //some code
} //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'
{
    //some code
} //does not compile

再过一段时间后,我尝试了以下编译良好的方法。

template <typename T>
int getSize(typename LL<T>::template Node<T>* node)
{
    //some code
}

现在,当我尝试从驱动程序函数调用此函数时,再次出现编译器错误:

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

我尝试了一切可能且无法解决的问题。有人可以解释一下发生了什么吗?

评论
  • Giles
    Giles 回复

    getSize(ll.head()) fails because of non-deduced context; template parameter T can't be deduced automatically.

    1)使用限定ID指定的类型的嵌套名称说明符(范围解析运算符::的所有内容):

    作为替代方案,您可以显式指定模板参数。

    getSize<int>(ll.head())
    

    Other issues, (1) Node is declared as private; (2) Node is declared as non-template but in the declaration of getSize it's used as template.

    See Where and why do I have to put the “template” and “typename” keywords? about why use the keyword typename and template.