我有一段代码在 gcc 下编译没有问题但在 g++ 下没有问题。我正在转向 g++,因为我想利用 boost 来改进一些多线程。我得到的错误是这样的:
invalid conversion from 'void* volatile' to 'TrialVect*'
这段代码是:
static void funcSize( struct TrialVect *vector ) {
if ( vector->field ) {
struct TrialVect *ptr = BO2V (vector->first,vector->second);
}
}
正如我在谷歌上搜索的那样,转换变量存在一些问题,为什么?建议如何解决?
最佳答案
在 C++ 中,任何指针都可以隐式地转换为到 void*
,但是从 void*
转换需要一个显式转换。
试试这个:
auto ptr = static_cast<TrialVect* volatile>( BO2V (vector->first,vector->second) );
volatile
是必须单独处理的属性(如const
);新指针必须与旧指针匹配,或者您可以使用单独的 const_cast
更改 const
/volatile
属性。例如,
auto ptr = static_cast<TrialVect*>( const_cast<void*>( BO2V(vector->first, vector->second) ) );
删除 volatile
属性是否安全取决于应用程序。
关于c++ - 来自“void*”的无效转换 - 类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465812/