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;
}
I get the following error:
error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
Why? How can I fix this problem?
std::unique_ptr, which is impossible. You have to either move from it or bind it to a reference. - 0x499602D2auto c = std::move(children);in both places. And we can't tell you why it compiles in once place and not the other because you haven't provided an SSCCE. - Praetorian#include <...>lines, but this is far more than we normally get. - Bill Lynch