Pls Tell me how to assign and and access the boost variant containing the vector and structure

Mar 5, 2012 at 1:01pm
Dear friends,
Can any one tell me how come out of this problem.


in my big code I am using the following simple code, before I porting in to my project code I were tested like individual module wise but unfortunately the following module is not working , please could any one can help out of this problem.

code::
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
#include<iostream>
#include<vector>
#include<string>
#include<boost/variant.hpp>

using namespace std;

typedef vector<int> v;
struct address
{
int val;
string type;
};
typedef struct address addr;

struct link
{
boost::variant<v> vari;
boost::variant<addr> address;
};
typedef struct link l1; 
int main()
{
addr. val=10;
addr.type="link";

l1.address = addr;

l1.vari.push_back(1);


cout << " content of vector" << l1.vari <<  endl;
cout <<" content of structure" << l1.address.val << l1.address.type << endl;
}
Mar 5, 2012 at 1:03pm
error for the following code is:::

sr/include/boost/variant/detail/variant_io.hpp: In member function ‘void boost::detail::variant::printer<OStream>::operator()(const T&) const [with T = std::vector<int>, OStream = std::basic_ostream<char>]’:
/usr/include/boost/variant/variant.hpp:858:32: instantiated from ‘boost::detail::variant::invoke_visitor<Visitor>::result_type boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&, int) [with T = const std::vector<int>, Visitor = boost::detail::variant::printer<std::basic_ostream<char> >, boost::detail::variant::invoke_visitor<Visitor>::result_type = void]’
/usr/include/boost/variant/detail/visitation_impl.hpp:145:13: instantiated from ‘typename Visitor::result_type boost::detail::variant::visitation_impl_invoke_impl(int, Visitor&, VoidPtrCV, T*, mpl_::false_) [with Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >, VoidPtrCV = const void*, T = std::vector<int>, type
Mar 5, 2012 at 1:46pm
to access a value of the variant you need to use boost::get<...>() : boost::get<v>(l1.vari).push_back(1); in your case.

Why do you use variants with just a single type? That doesn't make too much sense?
Mar 5, 2012 at 2:10pm
Thanking you so much , here I have written only single type value but actually in my code multiple values were declared .I will test and I get back to you if any problem.
Mar 5, 2012 at 2:23pm
Dear friend,

again the following error is coming.

modified code:
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
using namespace std;

typedef vector<int> v;
struct address
{
int val;
string type;
};
typedef struct address addr;

struct link
{
boost::variant<v> vari;
boost::variant<addr> address;
};
typedef struct link l1;
int main()
{
boost::get<addr>(addr. val=10);
boost::get<addr>(addr.type="link");

boost::get<addr>(l1.address = addr);

boost::get<v>(l1.vari).push_back(1);


cout << " content of vector" << l1.vari <<  endl;
cout <<" content of structure" << l1.address.val << l1.address.type << endl;
}


error::


1
2
3
4
5
6
7
8
variant1.cpp:24:22: error: expected primary-expression before ‘.’ token
variant1.cpp:25:22: error: expected primary-expression before ‘.’ token
variant1.cpp:27:20: error: expected primary-expression before ‘.’ token
variant1.cpp:27:35: error: expected primary-expression before ‘)’ token
variant1.cpp:29:17: error: expected primary-expression before ‘.’ token
variant1.cpp:32:35: error: expected primary-expression before ‘.’ token
variant1.cpp:33:37: error: expected primary-expression before ‘.’ token
variant1.cpp:33:55: error: expected primary-expression before ‘.’ token
Mar 5, 2012 at 2:41pm
The boost::get() function is only for getting a value from a variant because variant cannot guess what type to return.

on line 22 you don't need get since the type left of '=' makes clear for the compiler which type to use. Also it's wrongfully used: boost::get<addr>(l1.address = addr); -> boost::get<addr>(l1.address) = addr;

line 19/20 doesn't have anything to do with variants so no boost::get(). The problem is that 'addr' is a type. You cannot access non static members just with the type. In other word you have to instantiate an object and then access the member variables from that object:
1
2
3
4
addr a;
a.val=10;
a.type="link";
l1.address = a;
Mar 5, 2012 at 3:03pm
boost::variant<v> vari;

What madness is this? Why do you want to use a variant at all, if there is only one type - the type can't vary?

On a reasonable variant, use boost::get<>().
Tutorial: http://www.boost.org/doc/libs/1_49_0/doc/html/variant/tutorial.html

Example:
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
#include <boost/variant.hpp>
#include <iostream>
#include <string>

int main()
{
    boost::variant< int, std::string > v ;

    v = 98 ;
    int i = boost::get<int>(v) ;
    std::cout << i << '\n' ;
    boost::get<int>(v) = 9999 ;

    std::cout << boost::get<int>(v) << '\n' ;

    v = "hello world" ;
    std::string& str = boost::get<std::string&>(v) ;
    std::cout << str << '\n' ;

    try
    {
        i = boost::get<int>(v) ;
        std::cout << "if this message is printed, stop using boost::variant.\n" ;
    }
    catch( const boost::bad_get& bg )
    {
        std::cerr << bg.what() << '\n' ;
    }
}
Mar 5, 2012 at 3:48pm
Thanks brother for your sharp and powerful answer , I have posted only part of my big code just for the sake of porting into my big code. Actually in my real boost variant have multiple types ( sorry for misunderstanding ).
Mar 5, 2012 at 4:07pm
Dear Friend ,
Again it is giving same error, could you look pls.

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

using namespace std;
typedef vector<int> v;
struct address
{
int val;
string type;
};
typedef struct address addr;

struct link
{
boost::variant<v> vari;
boost::variant<addr> address;
};
typedef struct link l1;
int main()
{
addr a;
a.val=10;
a.type="link";
l1.address = a;
l1.vari.push_back(1);


cout << " content of vector" << boost::get<v>(l1.vari).front() <<  endl;
cout << "content of structure" << boost::get<address>(l1.address.val) << boost::get<address>(l1.address.type) << endl;
}



Error:
1
2
3
4
5
6
variant2.cpp: In function ‘int main()’:
variant2.cpp:28:3: error: expected unqualified-id before ‘.’ token
variant2.cpp:29:3: error: expected unqualified-id before ‘.’ token
variant2.cpp:32:49: error: expected primary-expression before ‘.’ token
variant2.cpp:33:57: error: expected primary-expression before ‘.’ token
variant2.cpp:33:96: error: expected primary-expression before ‘.’ token




Mar 5, 2012 at 4:21pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//...

//*****************************************************
//typedef struct link l1;
// if you thought that  l1 in the line above was an object, you shouldn't be using boost.
//*****************************************************


int main()
{
    addr a;
    a.val=10;
    a.type="link";

    link l1; 
    
    l1.address = a;
    
    //l1.vari.push_back(1); 
    
    v& vec = boost::get<V&>(l1.vari) ; 
    vec.push_back(1) ;
    
    // etc. 
Mar 5, 2012 at 5:23pm
Dear friend;

I tried in different ways but still I am getting the error

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
using namespace std;
typedef vector<int> v;
struct address
{
int val;
string type;
};
typedef struct address addr;

struct link
{
boost::variant<v> vari;
boost::variant<addr> address;
};
//typedef struct link l1;
int main()
{
 link l1;
addr a;
a.val=10;
a.type="link";
l1.address = a;
v& vec = boost::get<v&>(l1.vari);
vec.push_back(1);
//l1.vari.push_back(1);


cout << " content of vector" << boost::get<v>(l1.vari).front() <<  endl;
cout << "content of structure" << boost::get<address>(l1.address.val) << boost::get<address>(l1.address.type) << endl;
}


error
1
2
3

variant2.cpp:25:6: error: expected ‘;’ before ‘l1’
variant2.cpp:29:1: error: ‘l1’ was not declared in this scope
Mar 5, 2012 at 8:46pm
You really need to understand that boost::get can only be applied on variant objects!

so on line 29 it is

cout << "content of structure" << boost::get<address>(l1.address).val << boost::get<address>(l1.address).type << endl;

did you get the idea what the different between type and object is?
Mar 6, 2012 at 6:01am
Thanks dear friend , It is working fine . but please tell where can I get boost libraries concepts except from boost website , you know in boost website there is no much information about application examples , and many things is not clear , or tell me if any optional suggestions also .

Mar 7, 2012 at 6:33am
Yes, I know that the documentation of boost is not the best, but unfortunately I don't know any alternatives
Mar 7, 2012 at 2:10pm
Thank you..
Mar 7, 2012 at 5:27pm
I suggest you to read this tutoriel first:
http://www.cplusplus.com/doc/tutorial/
and look at boost in 2 or 3 months when you know how to write proper c++.
Topic archived. No new replies allowed.