I have the following code:
#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>'
std::unique_ptr
。你必须要么从中移动其值,要么将其绑定到一个引用上。 - David Gauto c = std::move(children);
。我们无法告诉你为什么它在一个地方编译而在另一个地方没有,因为你没有提供 SSCCE。 - Praetorian#include <...>
语句,不过这已经比我们通常得到的要好得多了。 - Bill Lynch