这句代码哪里写错了:std::cout << *reinterpret_cast<float*>(&a) << " " << *reinterpret_cast<float*>(&a) != *reinterpret_cast<float*>(&a) << std::endl;
时间: 2024-02-18 08:01:53 浏览: 26
这句代码中存在一个运算符优先级的问题,具体来说,是 `!=` 运算符的优先级比 `<<` 运算符低,因此需要使用括号改变优先级,正确的代码应该是:
```
std::cout << *reinterpret_cast<float*>(&a) << " " << (*reinterpret_cast<float*>(&a) != *reinterpret_cast<float*>(&a)) << std::endl;
```
这样才能先计算 `!=` 运算符,再将结果和前面的输出一起传递给 `<<` 运算符进行打印。
相关问题
#include <iostream> #include <pcl/common/common_headers.h> #include <pcl/io/pcd_io.h> #include <pcl/visualization/pcl_visualizer.h> #include <pcl/visualization/cloud_viewer.h> #include <pcl/console/parse.h> int main(int argc, char** argv) { std::cout << "Test PCL !!!" << std::endl; pcl::PointCloudpcl::PointXYZRGB::Ptr point_cloud_ptr(new pcl::PointCloudpcl::PointXYZRGB); uint8_t r(255), g(15), b(15); for (float z(-1.0); z <= 1.0; z += 0.05) { for (float angle(0.0); angle <= 360.0; angle += 5.0) { pcl::PointXYZRGB point; point.x = 0.5 * cosf(pcl::deg2rad(angle)); point.y = sinf(pcl::deg2rad(angle)); point.z = z; uint32_t rgb = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b)); point.rgb = reinterpret_cast<float>(&rgb); point_cloud_ptr->points.push_back(point); } if (z < 0.0) { r -= 12; g += 12; } else { g -= 12; b += 12; } } point_cloud_ptr->width = (int)point_cloud_ptr->points.size(); point_cloud_ptr->height = 1; pcl::visualization::CloudViewer viewer("test"); viewer.showCloud(point_cloud_ptr); while (!viewer.wasStopped()) {}; return 0; }这个代码能在vs022和pcl1.13.1下运行吗
这段代码在VS2012和PCL 1.13.1下运行时会出现编译错误,原因在于以下几点:
1. 在 `pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr` 这一行中,`pcl` 和 `PointXYZRGB` 之间漏了一个点号。
2. 在 `point.rgb = reinterpret_cast<float>(&rgb)` 这一行中,使用了错误的类型转换方式,应该使用 `*reinterpret_cast<float*>(&rgb)`。
修改后的代码如下:
```
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
int main(int argc, char** argv) {
std::cout << "Test PCL !!!" << std::endl;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
uint8_t r(255), g(15), b(15);
for (float z(-1.0); z <= 1.0; z += 0.05) {
for (float angle(0.0); angle <= 360.0; angle += 5.0) {
pcl::PointXYZRGB point;
point.x = 0.5 * cosf(pcl::deg2rad(angle));
point.y = sinf(pcl::deg2rad(angle));
point.z = z;
uint32_t rgb = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
point.rgb = *reinterpret_cast<float*>(&rgb);
point_cloud_ptr->points.push_back(point);
}
if (z < 0.0) {
r -= 12; g += 12;
} else {
g -= 12; b += 12;
}
}
point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
point_cloud_ptr->height = 1;
pcl::visualization::CloudViewer viewer("test");
viewer.showCloud(point_cloud_ptr);
while (!viewer.wasStopped()) {};
return 0;
}
```
这个代码经过修改后,应该可以在VS2012和PCL 1.13.1下正常运行了。
#include <iostream> using namespace std; struct Base1{ int x; }; struct Base2{ float y; }; struct Derived : Base1,Base2{ }; int main(int argc, char** argv) { Derived* pd=new Derived; pd->x =1; pd->y =2.0f; void* pv= pd; Base2* pd=static_cast<Base2* >(pv); cout <<pd->y <<""<<pd->y <<endl; delete pb; return 0; }这个程序能得到预期结果吗?如何避免类似问题发生?
不,这个程序不能得到预期结果。因为在对指针进行类型转换时,指针类型必须与其所指向的对象类型相兼容,否则会导致未定义行为。
在这个程序中,由于将 `pd` 指针转换为 `Base2*` 指针,而 `pd` 实际上是 `Derived*` 指针,所以转换是不兼容的。所以输出的结果是未定义的。
为了避免这种类型转换问题,应该尽可能使用 C++ 中提供的安全类型转换方式,例如使用 `dynamic_cast` 或者 `reinterpret_cast`。同时,在代码编写过程中应该尽可能避免进行类型转换,尽量使用多态、虚函数等方式来实现代码逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)