编译器错误使用C ++的std ::阵矢量 [英] compiler error with C++ std::vector of array
问题描述
以下code没有用gcc 4.7.0编译(使用std = C ++ 11 -O3)
the following code doesn't compile with gcc 4.7.0 (using std=c++11 -O3)
int n;
std::vector< int[4] > A;
A.resize(n);
错误消息的长度,但最终
the error message is length, but eventually
functional cast to array type ‘_ValueType {aka int[4]}‘
这是正确的?或者应该这样编译?更重要的是,如何避免这个问题? (没有定义一个新的结构来保存 INT [4]
)
编辑:
如何解决与C ++ 98的问题?
how to solve the problem with C++98?
推荐答案
您不能存储阵列中的一个载体或任何其他容器。类型的元素的要被存储在一个容器(称为容器的值类型)必须既拷贝构造和可分配。数组是没有。
You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.
您可以,但是,使用数组类模板,像升压,TR1中提供的,和C ++ 0x中:
You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x:
std::vector<std::array<type, size> >
(你想的std :: TR1 ::阵列替换的std ::阵列使用包含在C ++ TR1模板,或升压::数组使用的模板从的Boost库或者,您也可以编写自己的。这是很简单的)
(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)
<一个href=\"http://stackoverflow.com/questions/4612273/correct-way-to-work-with-vector-of-arrays\">@source由詹姆斯McNellis
@source By:James McNellis
所以,code看起来像:
So the code would look like:
int n;
std::vector<std::array<int,3>> A;
A.resize(n);
这篇关于编译器错误使用C ++的std ::阵矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!