簡體   English   中英

無法訪問私有成員-模板和std :: unique_ptr

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM