I am trying to compile this library using MSVC10, and this function is giving me headache:
/*! \brief Read bytes from a \c std::istream
\param is The stream to be read.
\param data A pointer to a location to store the bytes.
\param size The number of bytes to be read.
*/
void _read(std::istream &is, unsigned char *data, int size)
{
for (int i=0; i < size ; ++i )
is.get(static_cast<char>(data[i]));
}
error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'char' to 'char &'
The original used static_cast, so I try with reinterpret_cast as suggested elsewhere but that fails too:
error C2440: 'reinterpret_cast' : cannot convert from 'unsigned char' to 'char'
This library comes with unix makefiles. What is the best way to resolve this compile error?
Because reinterpret_cast
does not work that way, by definition.
In order to perform memory re-interpretation, you have to apply reinterpret_cast
to pointers or references. If you want to reinterpret unsigned char
data as char
data, you actually have to convert to char &
type, not to char
type.
In your case that would be
is.get(reinterpret_cast<char &>(data[i]));
Or you can go the pointer route and do
is.get(*reinterpret_cast<char *>(&data[i]));
(which is the same thing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With