ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to deal with the bug:‘Bag’ is not a member of ‘ros’?

asked 2019-07-26 02:11:37 -0500

YancenBOB gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-07-26 03:43:59 -0500

kump gravatar image

updated 2019-07-26 04:01:39 -0500

In your code above, you show a c++ code where in your main function you create a rosbag instance by command ros::Bag bag;. This is causing the error

/home/cyx/test-catkin_ws/src/read/read.cpp:35:2: error: ‘Bag’ is not a member of ‘ros’
  ros::Bag bag;

because Bag is NOT member of ros. The Bag class is member of rosbag that you import on the line 2

#include <rosbag/bag.h>

The correct way to create rosbag instance is

rosbag::Bag bag;

NOT the

 ros::Bag bag;

See the difference?

edit flag offensive delete link more

Comments

@YancenBOB I have edited the answer for better clarity.

kump gravatar image kump  ( 2019-07-26 04:02:00 -0500 )edit

Thank you ! I am correct the spelling and close the question.

YancenBOB gravatar image YancenBOB  ( 2019-07-29 04:12:31 -0500 )edit

@YancenBOB On discussion forums like this one, answers.ros.org or any stack exchanges forums, for example, if some answer helped you solve your problem, you are supposed to click this check button

check button

by which you confirm your problem was solved. It is then visible from the list of questions. You don't close the question.

If your problem was not solved completely by the answer but the answer was helpful, you can give the answer an upvote by clicking the upward arrow above the number of upvotes (the zero above the check button in the picture).

kump gravatar image kump  ( 2019-07-29 04:29:27 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-07-26 02:11:37 -0500

Seen: 320 times

Last updated: Jul 26 '19