这段C++代码用VC6SP6编译怎么有错呢

来源:百度知道 编辑:UC知道 时间:2024/05/14 03:43:47
#include<iostream>
using namespace std;

int main()
{
float f=19.2F;
unsigned int* pa = reinterpret_cast<int*>(&f);
for(int i=31;i>=0;i--)
cout<<(*pa>>i & 1)<<(i==31 || i==23 ? "-":"");
cout<<"\n";
}

--------------------Configuration: jinzhi - Win32 Debug--------------------
Compiling...
jinzhi.cpp
icl: warning: problem with Microsoft compilation of 'E:\C++学习代码\jinzhi\jinzhi.cpp'
E:\C++学习代码\jinzhi\jinzhi.cpp(7): error: a value of type "int *" cannot be used to initialize an entity of type "unsigned int *"
unsigned int* pa = reinterpret_cast<int*>(&f);
^
compilation aborted for E:\C++学习代码\jinzhi\jinzhi.cpp (code 2)
Error executing xicl6.exe.

jinzhi.exe - 1 error(s), 1 warning(s)
这是书上的原代码 没改过 书上不能有错把

int类型指针不能隐式转换位uint类型指针,可能会丢失精度,用显式转换看看,或者修改一下其中一个的类型,两边一致即可

PS:书上的例子在实际操作中出问题是正常的,比如运行环境不一样,或者编译设置不一样都有可能造成出错;也许这个例子到了其他编译器里就只有一个警告,但在VC里却是一个错误

根据错误信息应该是这句出错。
unsigned int* pa = reinterpret_cast<int*>(&f);

大概的意思是 类型为"int *" 的值不能初始化成一个实体类型"unsigned int *" 。
C++我懂得不多,抱歉