ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Compiler error when trying to read message values

asked 2017-08-15 03:35:44 -0500

Sergey Kravchenko gravatar image

updated 2017-08-15 03:36:22 -0500

Here is my SWrite.msg file

int16 id
int16 pos
int16 time

Here is my CMakeLists.txt


cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

add_message_files(FILES SWrite.msg)

generate_messages(DEPENDENCIES std_msgs)


include_directories(include ${catkin_INCLUDE_DIRS} )

add_executable(scs_listener src/scs_listener.cpp)
add_library(hardwareserial src/HardwareSerial.cpp)
add_library(scservo src/SCServo.cpp)
add_library(scsprotocol src/SCSProtocol.cpp)
target_link_libraries(scs_listener ${catkin_LIBRARIES} hardwareserial scservo scsprotocol)
add_dependencies(scs_listener beginner_tutorials_generate_messages_cpp)

# %EndTag(FULLTEXT)%

And here is my callback function:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
  int id = msg->id;

I get compiler error with this:

/home/serge/NetBeansProjects/ROS_beginner_tutorials/ros_ws/src/scs_servo_control/src/scs_listener.cpp: In function ‘void chatterCallback(const ConstPtr&)’:
/home/serge/NetBeansProjects/ROS_beginner_tutorials/ros_ws/src/scs_servo_control/src/scs_listener.cpp:26:19: error: ‘const struct std_msgs::String_<std::allocator<void> >’ has no member named ‘id’
     int id = msg->id;
scs_servo_control/CMakeFiles/scs_listener.dir/build.make:62: recipe for target 'scs_servo_control/CMakeFiles/scs_listener.dir/src/scs_listener.cpp.o' failed

It seems to be logical, because id (and also pos and time) is nowhere defined as member.

But I read this

and this

and it confuses me.

So how can I read values from a message in my callback function? What I missed?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-08-15 04:11:27 -0500

gvdhoorn gravatar image

updated 2017-08-15 04:11:59 -0500

And here is my callback function:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
  int id = msg->id;

you define a custom message (SWrite.msg), but in your callback signature you still appear to be using std_msgs::String. That doesn't make sense to me.

The error message ("there is no field id in std_msgs::String") does make sense in that case.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2017-08-15 03:35:44 -0500

Seen: 782 times

Last updated: Aug 15 '17