Ask Your Question

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

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

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 (

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::NodeHandle n; 

ros::Bag bag;"test.bag", rosbag::bagmode::Read);

std::vector<std::string> topics;


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;



Here is the CMakeListst:

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS roscpp rosbag  std_msgs)
find_package(Boost REQUIRED program_options filesystem)


  CATKIN_DEPENDS roscpp rosbag std_msgs


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">
  <description>The bagread package</description>





  <!-- The export tag contains other, unspecified, tags -->
    <!-- Other tools can request additional information be placed here -->


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

1 Answer

Sort by » oldest newest most voted

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

kump gravatar image

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

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


@YancenBOB I have edited the answer for better clarity.

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

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

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

@YancenBOB On discussion forums like this one, 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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



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

Seen: 91 times

Last updated: Jul 26 '19