error: ISO C++ forbids declaration of ‘point’ with no type [-fpermissive]

文章目录

  • 前言
  • 一、UBUNTU下C++编译
  • 二、报错
    • 1.查看CMakeLists.txt
    • 2.报错
    • 3.修改后的CMakeLists.txt


前言

学习点云PCL过程中,跟着教程敲代码。
编译pcd_write 的时候,遇到这个问题。

自己系统:
UBUNTU16 尝试了vscode 和 默认终端两种编译方式,报错方式相同。


一、UBUNTU下C++编译

ubuntu下c++编译 需要 CMakeLists.txt 文件,同时新建 build ,通过cmake make 编译可执行文件。
例:

mkdir build
cd build
cmake .. // 对上一级进行编译
make  // 生成可执行文件命令
./pcd_write  // 运行  生成pcd文件并打印5个点的值

二、报错

1.查看CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
 
project(pcd_write)
 
find_package(PCL 1.2 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable (pcd_write pcd_write.cpp)
target_link_libraries (pcd_write ${PCL_LIBRARIES})

2.报错

报错如下

error: ISO C++ forbids declaration of ‘point’ with no type [-fpermissive]
   for (auto& point: cloud)
warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
   for (auto& point: cloud)
error: request for member ‘y’ in ‘point’, which is of non-class type ‘int’
     point.y = 1024 * rand () / (RAND_MAX + 1.0f);
error: request for member ‘y’ in ‘point’, which is of non-class type ‘int’
     point.y = 1024 * rand () / (RAND_MAX + 1.0f);
error: request for member ‘z’ in ‘point’, which is of non-class type ‘int’
     point.z = 1024 * rand () / (RAND_MAX + 1.0f);

由第二行可知,for循环仅适用于-std=c++11 或 -std=gnu++11,所以需要在CMakeLists.txt文件中加入C++11标准最终的CMakeLists.txt文件内容如下:

3.修改后的CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
 
# 指定为C++11 标准
set(CMAKE_CXX_STANDARD 11)
 
project(pcd_write)
 
find_package(PCL 1.2 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable (pcd_write pcd_write.cpp)
target_link_libraries (pcd_write ${PCL_LIBRARIES})

接下来即可顺利编译。


你可能感兴趣的:(点云PCL学习,c++,ubuntu,自动驾驶)