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

Create a std::vector of msgs

asked 2015-03-17 05:17:50 -0600

kokirits gravatar image

updated 2015-03-18 02:59:26 -0600

Hello all,

I'm trying to create a vector of a custom msg that I have declared. The msgs contains the following line:

float64[] LuT //where LuT is a look up table I want to publish as a msg.

But when I'm trying to create a std::vector of msgs like so:

std::vector < parse_and_pub::Lookuptable::LuT>  tables;

Of course including the generated header:

#include "parse_and_pub/Lookuptable.h"

I get the following errors:

/home/name/hydro_ws/src/parse_and_pub/src/ParseAndPub.h:32: error: ‘parse_and_pub::Lookuptable_<std::allocator<void> >::LuT’ cannot appear in a constant-expression

/home/name/hydro_ws/src/parse_and_pub/src/ParseAndPub.h:32: error: template argument 1 is invalid

/home/name/hydro_ws/src/parse_and_pub/src/ParseAndPub.h:32: error: template argument 2 is invalid

Any idea If I'm doing something wrong, or if this is intended ?

Edit: creating a:

std::vector < parse_and_pub::LookuptableConstPtr> tables;

works. But how can I access and fill this vector? tables[index].push_back() doesn't work.

Edit2: Thank you all for your answers, but it's not quite what I wanted. I tried a different approach and seems to work for now.

1) declared the vector of msgs as:

std::vector <parse_and_pub::Lookuptable> tables;

2) populate the vectors like so:

for(...;...;....){ //iterate the outer std::vector
    parse_and_pub::Lookuptable temp; //a temp to store the i-th look up table
    for(...;...;....){ //iterate the inner vector
          //fill the temp variable
          temp.LuT.push_back(some_value);
    }
    tables.push_back(temp);//push back the temp variable in the outer vector
}
edit retag flag offensive close merge delete

Comments

1

Do you want to create a vector of your message or a vector of doubles? std::vector < parse_and_pub::LookuptableConstPtr> tables; is a vector of messages. std::vector<double> tables would be the equivalent to float64[] LuT

BennyRe gravatar image BennyRe  ( 2015-03-17 05:58:22 -0600 )edit

@BennyRe , thanks for answering. I want to create a vector of messages. But by using

 std::vector < parse_and_pub::LookuptableConstPtr> tables;

I don't know how to fill the message array.

kokirits gravatar image kokirits  ( 2015-03-17 06:15:43 -0600 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2015-03-17 08:56:36 -0600

Wolf gravatar image

updated 2015-03-17 10:24:39 -0600

Your message contains an array, so you do not need to create and array your self. If you have instantiated it, it already contains the array (which for C++ is serialized as std::vector). Use it like:

parse_and_pub::Lookuptable your_msg_obj;  // declare message instance

// fill lookup table
for ( /* all the values you want to add */ )
{
    your_msg_obj.LuT.push_back( 12.5 /* or whatever double value */ ); 
                          //  your_msg_obj.LuT is of type std::vector<double>
                          //  just treat it like you would thread any other vector of that kind 
                          //     i. e. use .resize(), .push_back(), operator[] on it or 
                          //                   derive iterators on it as you like
}

// and publish your message with filled look up table vector
your_publisher.publish( your_msg_obj );

Update for your comment:

For creating an array of msgs you can do:

std::vector<parse_and_pub::Lookuptable> your_luts;

your_luts.resize(1);
your_luts[0].LuT.push_back( 12.5 );

However, you won't be able to publish such a vector (also not if you use share_ptrs to messages). If you want to publish an arrary of such look up tables you must have another message type containing an array of your lookup tables:

Your Lookuptable.msg:

float64[] Lut

Your 2nd LookuptablesArray.msg:

parse_and_pub/Lookuptable[] look_up_tables ## array of Lookuptable.msg

Then you could create an instance of parse_and_pub::LookuptablesArray fill that and publish that.

edit flag offensive delete link more

Comments

Thanks for answering Wolf. What I want to do is to create a vector of vectors (were the second vector is what you are explaining above). Alternatively I want to create a vector of msgs (where each message is a vector). I hope it's more clear this way.

kokirits gravatar image kokirits  ( 2015-03-17 09:33:04 -0600 )edit

Great answer Wolf, I didn't even know about publishing a msg like:

parse_and_pub/Lookuptable[] look_up_tables ## array of Lookuptable.msg
kokirits gravatar image kokirits  ( 2015-03-18 03:01:11 -0600 )edit
1

answered 2015-03-17 07:23:16 -0600

BennyRe gravatar image

To create a vector of messages use

std::vector < parse_and_pub::LookuptableConstPtr> tables;

to populate the look up table use

tables.at(<index>).LuT.push_back(<value>);
edit flag offensive delete link more

Comments

I did this:

tables.at(index_prepro).LuT.push_back(atof(token));

but I get the error:

/home/name/hydro_ws/src/parse_and_pub/src/ParseAndPub.cpp:294: error: ‘class boost::shared_ptr<const parse_and_pub::Lookuptable_<std::allocator<void> > >’ has no member named ‘LuT’
kokirits gravatar image kokirits  ( 2015-03-17 07:31:39 -0600 )edit

Use -> instead of '.' You also need to make sure that the pointer exists already.

dornhege gravatar image dornhege  ( 2015-03-17 07:55:42 -0600 )edit

@dornhege -> doesn't work also

kokirits gravatar image kokirits  ( 2015-03-17 08:19:39 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-03-17 05:17:50 -0600

Seen: 3,931 times

Last updated: Mar 18 '15