Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法访问私有成员-模板和std::unique\u ptr_C++_Templates_C++11_Unique Ptr - Fatal编程技术网

C++ 无法访问私有成员-模板和std::unique\u ptr

C++ 无法访问私有成员-模板和std::unique\u ptr,c++,templates,c++11,unique-ptr,C++,Templates,C++11,Unique Ptr,我有以下代码: #include <memory> template<typename T, size_t Level> class Foo { friend class Foo<T, Level + 1>; typedef std::unique_ptr<T> ElmPtr; typedef std::unique_ptr<Foo<ElmPtr, Level - 1>> NodePtr;

我有以下代码:

#include <memory>

template<typename T, size_t Level>
class Foo
{
    friend class Foo<T, Level + 1>;
    typedef std::unique_ptr<T> ElmPtr;
    typedef std::unique_ptr<Foo<ElmPtr, Level - 1>> NodePtr;        

    public:
    Foo() {
        // no errors
        auto c = children;
    }

    Foo(int n) {
        // !!! compiler error !!!
        auto c = children;
    }

    std::array<NodePtr, 4> children;            
};

template<typename T>
class Foo<T, 0>
{
    friend class Foo<T, 1>;

    public:
    Foo() {}
};

int main()
{
    Foo<int, 1> foo1;
}
#包括
模板
福班
{
朋友班富;
typedef std::unique_ptr ElmPtr;
typedef std::unique_ptr NodePtr;
公众:
Foo(){
//没有错误
自动c=儿童;
}
Foo(内部网络){
//!!!编译器错误!!!
自动c=儿童;
}
std::数组子元素;
};
模板
福班
{
朋友班富;
公众:
Foo(){}
};
int main()
{
富富1;
}
我得到以下错误:

错误C2248:“std::unique\u ptr::unique\u ptr”:无法访问 在类“std::unique\u ptr”中声明的私有成员

为什么??如何解决此问题?

您有:

auto c = children;
其中:

std::array<std::unique_ptr<T>, N> children;            

两个构造函数都给了我一个错误,这是因为您试图复制一个
std::unique\u ptr
,这是不可能的。您必须从它移动或将它绑定到引用在两个位置。我们不能告诉你为什么它只在一个地方编译而不在另一个地方编译,因为你没有提供一个。@Praetorian:这怎么不是SSCCE?它很短,可以编译。遗憾的是,它缺少正确的
#include
行,但这远远超过了我们通常得到的。g++4.8..2显示了默认构造函数中的错误,too@Nick:问题是你提供的代码没有给出你在我能找到的任何编译器中描述的错误。不是叮当声,不是GCC,甚至不是MSVC。SSCCE中的第二个C代表“Correct”,意思是“示例……导致需要解决的问题的确切错误消息。”如果没有这个,我们将在这里解决代码,而修复对您毫无帮助。
auto& c = children; // OK