0

我对 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&);
};
4

1 回答 1

2

The problem is Arrays copy ctor is private: Array(const Array&); // no copy or assignment and std::async is trying to make a copy of sscm which contains an Array as a member. Wrap it in a std::reference_wrapper:

auto future = std::async(testTest, std::ref(sscm));
于 2013-04-10T04:41:34.100 回答