Dže*_*nan 4 c++ unix visual-studio-2010
我正在尝试使用MSVC10 编译这个库,这个函数让我很头疼:
/*! \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]));
}
Run Code Online (Sandbox Code Playgroud)
错误C2664:'std :: basic_istream <_Elem,_ Traits>&std :: basic_istream <_Elem,_Traits> :: get(_Elem&)':无法将参数1从'char'转换为'char&'
原来使用的是static_cast,所以我按照其他地方的建议尝试使用reinterpret_cast但是也失败了:
错误C2440:'reinterpret_cast':无法从'unsigned char'转换为'char'
该库附带unix makefile.解决此编译错误的最佳方法是什么?
AnT*_*AnT 10
reinterpret_cast根据定义,因为不起作用.
为了执行内存重新解释,您必须应用于reinterpret_cast指针或引用.如果要将unsigned char数据重新解释为char数据,则实际上必须转换为char &类型,而不是char键入.
在你的情况下,将是
is.get(reinterpret_cast<char &>(data[i]));
Run Code Online (Sandbox Code Playgroud)
或者你可以去指针路线做
is.get(*reinterpret_cast<char *>(&data[i]));
Run Code Online (Sandbox Code Playgroud)
(这是一回事).
| 归档时间: |
|
| 查看次数: |
7071 次 |
| 最近记录: |