How to Print the content of boost varint of vector

Dear , pls tell me how to print the content of variant of vector.ie just I want to print content of motor_action_rsp_list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<boost/variant.hpp>
#include<boost/variant/get.hpp>
#include<vector>
using namespace std;

struct motor_scan_rsp {

int net_id;
int signal;
};
typedef vector<motor_scan_rsp> motor_scan_rsp_list;

 struct motor_action_rsp {

boost::variant<motor_scan_rsp_list> scan_list;
friend std::ostream& operator << (std::ostream& os, motor_action_rsp& lar)
{
os << "net_id" << boost::get<motor_scan_rsp_list>(lar.scan_list)[0];
os << "rssi" << boost::get<motor_scan_rsp_list>(lar.scan_list)[1];
return os;
}
};
typedef vector<motor_action_rsp> motor_action_rsp_list;


int main()
{


motor_scan_rsp lsr;

motor_scan_rsp_list lsrl;
lsr.net_id = 202;
lsr.signal = 70;
lsrl.push_back(lsr);

motor_action_rsp lar;
motor_action_rsp_list larl;
for(motor_action_rsp_list::iterator it = larl.begin(); it != larl.end(); ++it)
{
cout << it;
}

Error :: is 

Unmatched Operator<< 
and big message is coming.
trycout << *it?
Last edited on
Hi Clamjc,
1
2
                       Same Error 
                                              No Match operator<<in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & os), ((const char*)"net_id")) << (& boost::get [with U = std::vector<motor_scan_rsp>, T0 = std::
what does boost::get<motor_scan_rsp_list>(lar.scan_list)[0] return? Maybe you can use boost::lexical_cast and do this...
 
boost::lexical_cast<std::string>(boost::get<motor_scan_rsp_list>(lar.scan_list)[0]) // assuming that works with lexical cast hence what does that return question 
1
2
3
4
 Hi,
                   I am bit confusing over the above statement only ..what are the other ways to print the content of vector which is  inside of boost variant is the question, this is I 'm exactly confusing .

ie  I want to print the net_id and signal by using boost varint of <motot_scan_rsp_list>  
1
2
3
4
5
6
friend std::ostream& operator << (std::ostream& os, motor_action_rsp& lar)
{
os << "net_id" << boost::get<motor_scan_rsp_list>(lar.scan_list)[0];
os << "rssi" << boost::get<motor_scan_rsp_list>(lar.scan_list)[1];
return os;
}


When you index here at lar.scan_list[0] you are in the context of the vector element and not the net_id, same thing for [1], that is not rssi that is the next element in the vector. Why not try this.

1
2
3
4
5
6
7
8
9
10
11
12
friend std::ostream& operator << (std::ostream& os, motor_action_rsp& lar)
{
os <<  boost::get<motor_scan_rsp_list>(lar.scan_list)[0];
return os;
}

std::ostream& operator<<(std::ostream& os, const motor_scan_rsp& msr)
{
   os << "net_id " << msr.net_id;
   os << "rssi" << msr.signal;
   return os;
};


I don't know if this fixes your problem, maybe can you post all the errors you are getting?

As a side note, why are you using variant here when it doesn't look like you have use for a union (unless this is part of code not posted).
Last edited on
Topic archived. No new replies allowed.