我查看了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
如何获得指向'locks'的指针?
答案 0 :(得分:3)
错误消息实际上是免费提供解决方案:
std::recursive_mutex * (*locks_2)[2] = &locks;
答案 1 :(得分:2)
人们可以使用这样一个事实:这种事情已被官方宣布为难以理解,因为没有明显的理由让编译器无法为你解决。如果您的编译器支持C ++ 2011。使用:
auto my_ptr = &locks; // point to locks, let the compiler worry about types
要求您将代码编译为C ++ 2011。 (GCC的-std=c++11
)。
答案 2 :(得分:1)
locks
只是一个指针数组。所以你可以用指针指针指向它。
std::recursive_mutex **locks_2 = locks;