无法从const std :: string转换为void *

时间:2012-09-27 06:43:50

标签: casting void-pointers stdstring

我有一个字符串和整数的向量 std :: vector strvect std :: vector intvect 当我从这些类型转换为void *时,从字符串转换会失败到它所经过的整数。有什么原因吗?

void *data; 
data = (void *)(strvect .front()); 
// gives me the error "Cannot cast from string to void * " 
data = (void *)(intvect.front());

任何具体原因?

1 个答案:

答案 0 :(得分:1)

非指针值无法转换为void指针。所以你要么必须使用address-of operator

data = reinterpret_cast<void*>(&strvect.front());

或者你得到实际的C字符串指针

data = reinterpret_cast<void*>(strvect.front().c_str());
相关问题