繁体   English   中英

在专用模板中访问嵌套朋友类的私有成员

[英]Access private member of nested friend class in specialized template

以下代码无法在Visual Studio 2103 Express预览中编译:

template<int N> class TTOuter;

template<>
class TTOuter<1>
{
public:
    class inner
    {
        friend class TTOuter<1>;

    private:
        inner(int n) : data(n) {;}
        int data;
    };

private:
    inner x;

public:
    TTOuter(int n) : x(n) {;} //Fails here
};

错误:无法访问“ TTOuter <1> :: inner :: inner(int n)”

如果外部类不是专用模板,则类似访问成功:

class Outer
{
public:
    class inner
    {
        friend class Outer;

    private:
        inner(int n) : data(n) { ; }
        int data;
    };

private:
    inner x;

public:
    Outer(int n) : x(n) { ; }
};

没有错误。

我试着像下面这样声明TTOuter <1>:

template<> class TTOuter<1>;

我还尝试通过以下方式替换朋友声明:

template<int N> friend class TTOuter;

但是两者都不起作用。

将不胜感激。 谢谢。

我认为这只是您的代码中的错字

public:
    TOuter(int n) : x(n) {;} //Fails here
  //^^^^^^ Shouldn't it be TTouter???
};

编辑:

如果我稍微编辑一下您的代码:

class TTOuter<1>
{
public:
    typedef TTOuter this_type; // <-- I add this typedef
    class inner
    {
        friend class this_type; // <-- Intellisense works
        //friend class TTOuter<1>; // <-- intellisense barks
        inner(int n) : data(n) { ; }
        int data;
    };
    TTOuter(int n) : x(0) {}
};

现在,intellisense不再抱怨。

暂无
暂无

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

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