我正在尝试创建一个向量,我将在C ++中存储不同类型的元素(在我的情况下是多维数组)。 我可以在向量中存储多维数组,但是当我尝试通过cast :: any检索时,我收到错误。让我用一个很简短的例子说明所有这些:
#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;
}
所以,当我运行它时,我收到以下错误:
“不匹配'运算符[]'(操作数类型为'__gnu_cxx :: __ alloc_traits&gt; :: value_type {aka boost :: any}'和'int')”
如果正确使用boost::any_cast
或我做错了什么的任何想法,将非常感谢。