1

I'm trying to print a specific integer value of a Bottle by using cout (the Bottle contains only integer values), but it seems that this is a wrong way to do it. The command I used in a for loop is (the b Bottle is defined outside the loop):

std::cout << b.get(i) << std::endl;

The corresponding error is:

enter image description here

I'd like to see an example regarding the reading of a Bottle value.

Gennaro Arguzzi
  • 769
  • 1
  • 14
  • 27

1 Answers1

2

You would have to get at the underlying type of the Value, if you know that it is indeed an int32_t (in other words b.get(i).isInt32() is true) then

std::cout << b.get(i).asInt32() << std::endl;

For the purposes of writing, without having to check the underlying type, you might also consider simply stringifying the Value

std::cout << b.get(i).toString() << std::endl;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Hello @CoryKramer thank you very much for your helpful answer. Can you tell me where to find the methods that I can use with get please (asInt, toString etc)? Why I can use two methods in sequence? – Gennaro Arguzzi Mar 27 '21 at 16:21
  • 1
    You can find them in the [`yarp::os::Value`](http://www.yarp.it/latest/classyarp_1_1os_1_1Value.html) documentation. – Daniele E. Domenichelli Mar 29 '21 at 17:07
  • 1
    Please note that using `asInt()` is not racommended in new code, you should use `asInt32()` instead. – Daniele E. Domenichelli Mar 29 '21 at 17:08
  • Hello @DanieleE.Domenichelli can you tell me the difference between asInt() and asInt32() please? Why asInt() is not reccomended? – Gennaro Arguzzi Mar 30 '21 at 08:05
  • 2
    @GennaroArguzzi Because `sizeof(int)` is platform-dependent, so to be sure the sender and receiver use an identical number of bytes you should use a fixed-width type like `int32` or `int64` as needed – Cory Kramer Mar 30 '21 at 11:25