vendredi 27 novembre 2015

Problems using std::vector with boost::any in C++

I am trying to create a vector where I will be storing different types of elements (in my case multidimensional arrays) in C++. I can store the multi-dimensional arrays in the vector but when I try to retrieve through cast::any I receive errors. Let me illustrate all these with a very short example:

#include <iterator>
#include <string>
#include <iostream>
#include <algorithm>
#include <list>
#include <boost/any.hpp>
#include "boost/variant.hpp"
#include "boost/multi_array.hpp"
using namespace std;
using blaze::DynamicMatrix;
using boost::multi_index_container;
using namespace boost::multi_index;

main(){

       //Creation of a 3-dimensional array of int 
       typedef boost::multi_array<int, 3> array_type;
       array_type C(boost::extents[3][4][2]);
       C[1][1][1] =222;

      //Creation of vector and insertion of the 3-d array in it
        std::vector<boost::any> vector;
        vector.push_back(C);

     // Trying to acess a stored element of the array from the vector
        cout << endl <<  "print element C[1][1][1] = " <<boost::any_cast<array_type24> (vector[0][1][1][1]) ;

return 1;
}

So, when I run this I receive the following error: "no match for ‘operator[]’ (operand types are ‘__gnu_cxx::__alloc_traits >::value_type {aka boost::any}’ and ‘int’)"

Any ideas how to use correctly the boost::any_cast or what I am doing wrong, would be very much appreciated.

Aucun commentaire:

Enregistrer un commentaire