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

I want to read an array of integers from a file and publish them on a topic

asked 2016-03-01 01:11:30 -0600

anonymous user

Anonymous

#

 ros::Publisher publisher = nh.advertise<std_msgs::Int32MultiArray>("Topic_1",1);

  std_msgs::Int32MultiArray vec;

  int i=0;

  ifstream infile("array1.txt");
  while(infile >> temp)
    {
      vec[i++] = temp;
      // publisher.publish(temp);
    }
  publisher.publish(vec);
  1. > #but I'm getting this error after I run the command $catkin_make in my > workspace

#

 ...
    [ 85%] [ 92%] [100%] Built target subscriber
    Building CXX object task/CMakeFiles/publisher_1.dir/src/publisher_1.cpp.o
    Building CXX object task/CMakeFiles/publisher_2.dir/src/publisher_2.cpp.o
    /home/kv/krssg/src/task/src/publisher_1.cpp: In function ‘int main(int, char**)’:
    /home/kv/krssg/src/task/src/publisher_1.cpp:27:10: error: no match for ‘operator[]’ (operand types are ‘std_msgs::Int32MultiArray’ and ‘int’)
           vec[i++] = temp;
              ^
    /home/kv/krssg/src/task/src/publisher_2.cpp: In function ‘int main(int, char**)’:
    /home/kv/krssg/src/task/src/publisher_2.cpp:25:11: error: no match for ‘operator[]’ (operand types are ‘std_msgs::Int32MultiArray’ and ‘int’)
           vec2[i++] = temp;
               ^
    make[2]: *** [task/CMakeFiles/publisher_1.dir/src/publisher_1.cpp.o] Error 1
    make[1]: *** [task/CMakeFiles/publisher_1.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs....
    make[2]: *** [task/CMakeFiles/publisher_2.dir/src/publisher_2.cpp.o] Error 1
    make[1]: *** [task/CMakeFiles/publisher_2.dir/all] Error 2
    make: *** [all] Error 2
    Invoking "make -j4 -l4" failed
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-03-01 01:39:09 -0600

mgruhler gravatar image

You are trying to acces the MultiArray directly over the [] operator. Check the message definition here.

You need to do a

vec.data[i++] = temp;

as, the data field is actually the vector representation. Also, make sure that the vector is big enough, so you don't get an index error. Use either the respective resize function or do push_back the new values.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-01 01:11:30 -0600

Seen: 436 times

Last updated: Mar 01 '16