[英]Unable to access private member - template and std::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;
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;
}
我收到以下错误:
错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员
为什么? 我该如何解决这个问题?
你有:
auto c = children;
哪里:
std::array<std::unique_ptr<T>, N> children;
这将需要复制unique_ptr
,而unique_ptr
是不可复制的。 不过,您可以参考children
:
auto& c = children; // OK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.