When I try to print the content of vector of structure of variant of vector the following big unbelievable error even it is very simple.
#include<iostream>
#include<boost/variant.hpp>
#include<vector>
#include<string>
usingnamespace std;
typedef std::string net_id;
typedef std::vector<net_id> netwk_id;
// Structure to hold the vector of netid's and some more information that I am not included here bcz it is //similar one.
struct network
{
boost::variant<netwk_id> _netwk_inc;
// operator overloading
friend std::ostream& operator<<(std::ostream& os, const network& nw)
{
os << " net_id " << boost::get<netwk_id>(nw._netwk_inc); // Here also error no
// match operator <<
}
};
// Here I am making vector containing the above structure // this vector only available at other side //meaning I am sending this vector through socket programming to some other server so that there I //have to decode or extract this information.
typedef vector<network> network_list;
int main()
{
// here I am assigning my netid to vector
netwk_id net1;
net1.push_back("202");
// here I am assigning vector to structure variable of type boost variant
network net2;
net2._netwk_inc = net1;
// finally I am pushing structure into vector
network_list net3;
net3.push_back(net2);
// Now see here i am trying to print the content of vector what ever may be I assigned earlier . but here //only it is showing error
// I don't know exactly how to extract the content
for(int i =0; i< net3.size(); ++i)
{
cout << net3[0]._netwk_inc ;
}
}
error :
// pls tell me how to get out of this proble
In file included from /usr/include/boost/variant/variant.hpp:1892:0,
from /usr/include/boost/variant.hpp:17,
from bin_query.cpp:2:
/usr/include/boost/variant/detail/variant_io.hpp: In member function ‘void boost::detail::variant::printer<OStream>::operator()(const T&) const [with T = std::vector<std::basic_string<char> >, OStream = std::basic_ostream<char>]’:
I think I already hinted at this in your other thread, look at what the template parameter you are trying to use...
std::vector<std::basic_string<char> >
How is it going to print a vector? You need to loop through the vector, or choose an element and print that, but not the vector.
Forgetting about variants for a second. Here is an example of what I'm talking about.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct MyStuff
{
std::string Hobby;
}
std::ostream& operator<<(std::ostream& os, const MyStuff& stuff)
{
os << stuff.Hobby;
return os;
}
//later in the code...
std::vector<MyStuff> vecMyStuff;
//... Push back 100 my stuff's..
os << vecMyStuff; //Error won't work would have had to have been os << vecMyStuff[2]; or something like that
Yeah ok that's not your problem, lets change MyStuff to look like yours...
struct MyStuff
{
std::vector<std::string> Hobbies;
}
std::ostream& operator<<(std::ostream& os, const MyStuff& stuff)
{
os << stuff.Hobbies; //Same problem as before, how does the stream know what to do with this, it doesn't.
return os;
}
//You have to tell it how to handle it. Here is an implementation, sparing optimizations...
std::ostream& operator<<(std::ostream& os, const MyStuff& stuff)
{
for(MyStuff::const_iterator it = stuff.begin(); it != stuff.end(); ++it)
{
os << *it; //If you just need one then you need to implement this the way you need it.
}
return os;
}
Note: Also looking at your code a second time, double check the "Types" you think you are passing to the overloaded ostream operator, they don't look like they match. hint: _netwk_inc != network
Hello Friend ,
I didn't understand what you mean about this line of code for(MyStuff::const_iterator it= stuff.begin(); it != stuff.end(); ++it) because how it possible to create iterator for structure Mystuff
You are right, that portion of @clanmjc is wrong. I'm sure he meant
1 2 3 4 5 6 7 8 9
std::ostream& operator<<(std::ostream& os, const MyStuff& stuff)
{
for(std::vector<std::string>::const_iterator it = stuff.Hobbies.begin(); it != stuff.Hobbies.end(); ++it)
{
os << *it; //If you just need one then you need to implement this the way you need it.
}
return os;
}
Please will you tell me if you know instead of vector in the above Mystuff structure has boost::variant< > var_name, now how to print see please variant is containing with vector instance
ie see
typedef vector<std::string> Hobbies
Struct Mystuff
{
boost::variant<Hobbies>var_name;
}
if it is like this means how to print is my real problem.
Actually my case is boost::variant <vector> var_name, but you had written above is vector of variant ie vector<boost::variant<>>
is both are same ? that's my doubt and please will you tell me I have other doubt.
ie how to type cast the boost::variant<vector> into unsigned char* ? any idea?
in my program error is giving when I try to assign this content of variant to a variable of type unsigned char* is shown bellow.
error: cannot convert ‘boost::variant<odtone::mih::null, std::vector<std::basic_string<char> > >’ to ‘unsigned char*’ in assignment