Prashant
Prashant

Reputation: 1180

Cannot cast from const std::string to void*

I have a vector of string and Integers something like std::vector strvect std::vector intvect when i am casting from these types to void * , casting from string fails where as to the integer it goes through. Any reason?

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

Any specific reason?

Upvotes: 2

Views: 2571

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

A non-pointer value can't be converted to a void pointer. So either you have to use the address-of operator

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

or you get the actual C-string pointer

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

Upvotes: 1

Related Questions