来自“void*”的无效转换 - 铸造
invalid conversion from 'void* - casting
我的代码有问题,在 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);
}
}
正如我在 Google 上搜索的那样,转换变量存在一些问题,为什么?建议如何解决?
在 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
属性是否安全取决于应用程序。
我的代码有问题,在 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);
}
}
正如我在 Google 上搜索的那样,转换变量存在一些问题,为什么?建议如何解决?
在 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
属性是否安全取决于应用程序。