错误C2248:'Array <t> :: Array':当某个类型的变量传递给std :: async </t> </t>时,无法访问在类'Array <t>'中声明的私有成员

时间:2013-04-10 04:28:43

标签: c++

我对C ++没有经验,我正在尝试修改一些C ++代码(特别是paq8l的代码)。我正试图通过std::async将其中一种数据类型作为参数传递。

int testTest(SmallStationaryContextMap &sscm)
{
    return 1;
}

int someOtherMethod()
{
    SmallStationaryContextMap sscm(0x20000);
    testTest(sscm); // fine
    auto future = std::async(testTest, sscm); // compiler error
}

我希望我只是遗漏了一些简单的东西,但是这给了我标题中的错误。 error C2248: 'Array<T>::Array' : cannot access private member declared in class 'Array<T>'

SmallStationaryContextMap:

class SmallStationaryContextMap {
    Array<U16> t;
    int cxt;
    U16 *cp;
public:
    SmallStationaryContextMap(int m): t(m/2), cxt(0) {
        assert((m/2&m/2-1)==0); // power of 2?
        for (int i=0; i<t.size(); ++i)
        t[i]=32768;
        cp=&t[0];
    }
    void set(U32 cx) {
        cxt=cx*256&t.size()-256;
    }
    void mix(Mixer& m, int rate=7) {
        *cp += (y<<16)-*cp+(1<<rate-1) >> rate;
        cp=&t[cxt+c0];
        m.add(stretch(*cp>>4));
    }
};

和数组:

template <class T, int ALIGN=0> class Array {
private:
    int n;     // user size
    int reserved;  // actual size
    char *ptr; // allocated memory, zeroed
    T* data;   // start of n elements of aligned data
    void create(int i);  // create with size i
public:
    explicit Array(int i=0) {create(i);}
    ~Array();
    T& operator[](int i) {
        return data[i];
    }
    const T& operator[](int i) const {
        return data[i];
    }
    int size() const {return n;}
    void resize(int i);  // change size to i
    void pop_back() {if (n>0) --n;}  // decrement size
    void push_back(const T& x);  // increment size, append x
private:
    Array(const Array&);  // no copy or assignment
    Array& operator=(const Array&);
};

1 个答案:

答案 0 :(得分:2)

问题是Array s copy ctor是私有的:Array(const Array&); // no copy or assignmentstd::async正在尝试制作sscm的副本,其中包含Array会员。将其包裹在std::reference_wrapper

auto future = std::async(testTest, std::ref(sscm));