我看了this excellent answer但无法弄清楚如何将其应用于此片段:
//this is in the .hpp file
std::atomic<int> size = 10;
std::recursive_mutex *locks[2];
//in some function of the class
//it's important that the 2nd array dimension is dynamic
the_lock[0] = new std::recursive_mutex[size];
the_lock[1] = new std::recursive_mutex[size];
std::recursive_mutex (*locks_2)[2][size] = &locks;
作业给我
error: cannot convert ‘std::recursive_mutex* (*)[2]’ to ‘std::recursive_mutex (*)
[2][(((sizetype)(((ssizetype)((**here be long type information, since I'm using
templates a lot**, long unsigned int, std::less<long unsigned int>
>::size.std::atomic<long unsigned
int>::<anonymous>.std::__atomic_base<_IntTp>::operator
std::__atomic_base<_IntTp>::__int_type<long unsigned int>()) + -1)) + 1)]’ in
initialization
如何获得指向“锁”的指针?
最佳答案
错误信息其实是免费赠送解决方案:
std::recursive_mutex * (*locks_2)[2] = &locks;
关于C++:如何获取指向二维动态数组的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458881/