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

How to write a callback function for Int8Multiarray messages subscription

asked 2013-07-25 08:28:54 -0500

Bholms gravatar image

I wrote a callback function for subscription for a int8Multiarray type array:

void arrayCallback(const std_msgs::Int8MultiArray::ConstPtr& array)
    int i = 0;
    int* p = array->data;
    for (i = 0; i < 10; i++)
        std::cout << array[i] << std::endl;

However, I cannot build the source code with this callback (I am positive that this is the only place that is causing error). The error in the prompt is:

/home/turtlebot/test2/src/GraphSub.cpp: In function ‘void arrayCallback(const boost::shared_ptr<const std_msgs::Int8MultiArray_<std::allocator<void> > >&)’:
  /home/turtlebot/test2/src/GraphSub.cpp:12: error: cannot convert ‘const std::vector<signed char, std::allocator<signed char> >’ to ‘int*’ in initialization
  /home/turtlebot/test2/src/GraphSub.cpp:15: error: no match for ‘operator[]’ in ‘array[i]’

Could anyone help me to eyeball the error?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2013-07-25 08:42:11 -0500

toniOliver gravatar image

Bear in mind that array->data is a std:vector, not a c++ array, so you can't assign it to a int*.

And to access the elements in the vector inside the loop you need to do:


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-07-25 08:28:54 -0500

Seen: 652 times

Last updated: Jul 25 '13