Ask Your Question
1

How does one assign values to messages with arrays in C++

asked 2020-10-19 18:56:43 -0500

cwillia109 gravatar image

This is probably more C++ focused, but here is my issue:

int8_t vi[height * width];
// vi has had work done on it between here
nav_msgs::OccupancyGrid debMap;
debMap.data = vi; // error here

The resulting error is:

no operator "=" matches these operands -- operand types are: std::vector<int8_t, std::allocator<int8_t>> = int8_t [height * width]

Now in python something like this would be an issue. In the past I have been using memcpy to get around this, but I am less certain that that is the approach to take for this.

How can I add array data to a message with an array.

edit retag flag offensive close merge delete

Comments

1

I believe, one brute force method is,

for (int i = 0; i<height*width; i++) {
  debMap.data.pushback(vi[i]);
}

I'm dunno if there is a way to directly assign it to data. May be treated debMap.data as std::vector and use one of the suggested methods here.

praskot gravatar image praskot  ( 2020-10-19 23:15:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-10-20 00:09:56 -0500

KenYN gravatar image

Here's one way to do it:

debMap.data.assign(vi, vi + height * width);

Cribbed from this answer on StackOverflow.

edit flag offensive delete link more

Comments

This worked like a charm. Thank you!

cwillia109 gravatar image cwillia109  ( 2020-10-22 12:54:49 -0500 )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

2 followers

Stats

Asked: 2020-10-19 18:56:43 -0500

Seen: 350 times

Last updated: Oct 20 '20