cannot convert parameter 1 from 'char *' to 'uint8_t *'
void OnReceived(std::shared_ptr<uint8_t> buffer, int len) {
.........
}
int main(){
std::vector<char> buffer(1000);
OnReceived((std::shared_ptr<uint8_t>)buffer.data(),rcvlen);
}
am trying to cast it but i cant i dont know why!!!
Error 1 error C2664: 'std::tr1::_Ptr_base<_Ty>::_Reset0' : cannot convert parameter 1 from 'char *' to 'uint8_t *' c:\program files\microsoft visua开发者_开发问答l studio 10.0\vc\include\memory 1705
so how can i convert it?
You really don't want to do that. Aside from the fact that char and uint8_t may be distinct types, even if you force the code to compile, your buffer will be deallocated twice, likely crashing your program. Just change OnReceived to accept a raw pointer.
Beyond the type mismatch, you don't want to do that. It'll almost cause a crash when both the vector destructor and the shared_ptr deallocate the same memory. You should consider boost::shared_array
or a shared pointer to the entire vector (shared_ptr<std::vector>
) instead of what you're doing now. Or as Igor suggests, OnReceived
could be changed to accept a raw pointer if it doesn't need shared ownership of the buffer.
uint8_t
is an unsigned char
so you'll either need to change the type of buffer or reinterpret_cast somewhere along the line.
buffer.data() will return a char *. You're trying to cast that to a std::shared_ptr - in other words, construct a shared_ptr out of it. In its implementation it tries to assign the pointer but can't since you're passing in an incompatible pointer type.
Try casting it to the appropriate type before passing it in, such as:
OnReceived((std::shared_ptr<uint8_t>)(uint8_t *)buffer.data(),rcvlen);
or, probably a better solution, convert your std::vector to a vector of uint8_t.
精彩评论