How to deal with the bug:‘Bag’ is not a member of ‘ros’?
Backgrounds: I am learning how to write/read a rosbag with C++ and I refer the Code API (see: link text). I create a rospackage and do some coding in Ubuntu 16.04 LTS and ROS Kinetic. But I meet some trouble.
Question: I run a easy demo but fail in the catkin_make. The terminal tells me that ‘Bag’ is not a member of ‘ros’. I am a beginner and write the CMakeLists.txt and package.xml with the reference of (https://github.com/lrse/dataset2bag/b...)
Here is my code:
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/String.h>
#include <std_msgs/Int32.h>
#include <iostream>
#include <vector>
#include <string>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
int main(int argc, char **argv)
{
ros::init(argc,argv,"hello");
ros::NodeHandle n;
ros::Bag bag;
bag.open("test.bag", rosbag::bagmode::Read);
std::vector<std::string> topics;
topics.push_back(std::string("chatter"));
topics.push_back(std::string("numbers"));
rosbag::View view(bag, rosbag::TopicQuery(topics));
foreach(rosbag::MessageInstance const m, view)
{
std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>();
if (s != NULL)
std::cout << s->data << std::endl;
std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>();
if (i != NULL)
std::cout << i->data << std::endl;
}
bag.close();
}
Here is the CMakeListst:
cmake_minimum_required(VERSION 2.8.3)
project(bagread)
find_package(catkin REQUIRED COMPONENTS roscpp rosbag std_msgs)
find_package(Boost REQUIRED program_options filesystem)
catkin_package(
CATKIN_DEPENDS roscpp rosbag std_msgs
DEPENDS Boost
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(hello hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${rosbag_LIBRARIES})
PS: I delete some ex;anatory note in the CMakeLists (with the mark #).
And here is the package.xml:
<?xml version="1.0"?>
<package format="2">
<name>bagread</name>
<version>0.0.0</version>
<description>The bagread package</description>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rosbag</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rosbag</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rosbag</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
And the error notes is here:
[ 50%] Building CXX object read/CMakeFiles/read.dir/read.cpp.o
/home/cyx/test-catkin_ws/src/read/read.cpp: In function ‘int main(int, char**)’:
/home/cyx/test-catkin_ws/src/read/read.cpp:35:2: error: ‘Bag’ is not a member of ‘ros’
ros::Bag bag;
^
/home/cyx/test-catkin_ws/src/read/read.cpp:35:2: note: suggested alternative:
In file included from /home/cyx/test-catkin_ws/src/read/read.cpp:2:0:
/opt/ros/kinetic/include/rosbag/bag.h:86:19: note: ‘rosbag::Bag’
class ROSBAG_DECL Bag
^
/home/cyx/test-catkin_ws/src/read/read.cpp:36:2: error: ‘bag’ was not declared in this scope
bag.open("test.bag", rosbag::bagmode::Read);
^
read/CMakeFiles/read.dir/build.make:62: recipe for target 'read/CMakeFiles/read.dir/read.cpp.o' failed
make[2]: *** [read/CMakeFiles/read.dir/read.cpp.o] Error 1
CMakeFiles/Makefile2:533: recipe for target 'read/CMakeFiles/read.dir/all' failed
make[1]: *** [read/CMakeFiles/read.dir ...