C ++错误:初始化结构实例的向量数组成员

时间:2015-08-19 11:15:50

标签: c++ vector struct

我有一个结构如下:

typedef struct
{
    vector<int[6]> swaps;
    int score;
}State;

然后我立即

State s1;
int s1arr[]={1,2,3,4,5,6};
s1.swaps.push_back(s1arr); //this is line number 164 in the error
s1.score=23;

以下所有内容都是一个巨大的错误。我找不到除了位置之外的任何地方的类似错误(不在堆栈溢出的地方也没有应答)。当我推回s1.swaps时会发生错误。如果有人可以帮我找出错误,我会很感激

错误记录

In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\vector:69:0,
                 from SessionOrganizer.h:13,
                 from SessionOrganizer.cpp:7:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc: In instantiation of
'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator
, const _Tp&) [with _Tp = int [6]; _Alloc = std::allocator<int [6]>; std::vector
<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int (*)[6], std::vector<i
nt [6]> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int (*)[6]]':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_vector.h:913:28:   required
from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = in
t [6]; _Alloc = std::allocator<int [6]>; std::vector<_Tp, _Alloc>::value_type =
int [6]]'
SessionOrganizer.cpp:164:33:   required from here
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc:329:19: error: array
must be initialized with a brace-enclosed initializer
    _Tp __x_copy = __x;
                   ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc:335:16: error: invali
d array assignment
    *__position = __x_copy;
                ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32\bits\c+
+allocator.h:33:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\allocator.
h:46,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:41,
                 from SessionOrganizer.h:10,
                 from SessionOrganizer.cpp:7:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h: In instantiation
 of 'void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>
::pointer, const _Tp&) [with _Tp = int [6]; __gnu_cxx::new_allocator<_Tp>::point
er = int (*)[6]]':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\alloc_traits.h:216:9:   required
from 'static void __gnu_cxx::__alloc_traits<_Alloc>::construct(_Alloc&, __gnu_cx
x::__alloc_traits<_Alloc>::pointer, const _Tp&) [with _Tp = int [6]; _Alloc = st
d::allocator<int [6]>; __gnu_cxx::__alloc_traits<_Alloc>::pointer = int (*)[6]]'

c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_vector.h:906:34:   required
from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = in
t [6]; _Alloc = std::allocator<int [6]>; std::vector<_Tp, _Alloc>::value_type =
int [6]]'
SessionOrganizer.cpp:164:33:   required from here
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h:130:9: error: par
enthesized initializer in array new [-fpermissive]
       { ::new((void *)__p) _Tp(__val); }
         ^
Makefile:2: recipe for target 'all' failed

3 个答案:

答案 0 :(得分:10)

您不应将C array存储在矢量中,它既不可复制,也不可移动,也不可分配。如果可以使用C ++ 11,可以使用std::array,否则使用boost::array。您也可以只使用std::vector<std::vector<int> >

vector<array<int, 6> > swaps;

答案 1 :(得分:0)

std::vector正在内部分配其元素,这不是为数组定义的。你错误的有趣部分是:

  

错误:无效的数组赋值

如果你写的话,你会得到同样的错误:

int nn[10];
int nn2[10];
nn = nn2;

这给出了:

main.cpp:21:8: error: invalid array assignment
     nn = nn2;
        ^

所以请使用vector的{​​{1}} - 或vector的{​​{1}} - s

答案 2 :(得分:0)

由于你不能使用c ++ 11,要么像其他人所建议的那样使用boost::array,要么使用向量向量,并使用数组初始化内部向量。

这是向量解决方案的向量 - 它在结构上与您想要的类似。

#include <vector>

struct State {
    std::vector<std::vector<int> > swaps;   // Note the space betweeen the 
                                            // angle brackets due lack of c++11
    int score;
};

int main(int argc, char** argv)
{
    int s1arr[] = {1, 2, 3, 4, 5, 6};
    State s1;
    s1.swaps.push_back(
        std::vector<int>(s1arr,                                       // Start of s1arr 
                         s1arr + sizeof(s1arr) / sizeof(s1arr[0])));  // End of s1arr
}

不可否认,这并不像你最初那样干净,但是,这可以变得更加清晰如下(如果你不介意将数组提供给构造函数):

#include <vector>

struct State {
    std::vector<std::vector<int> > swaps;
    int score;

    // Default constructor so you can still use the old way
    State {};

    // Constructor which will take an array and create the vector
    template<size_t N>
    State(const int (&array)[N]) 
    {
        swaps.push_back(std::vector<int>(array, array + N));
    }
};

int main(int argc, char** argv)
{
    int s1arr[] = {1, 2, 3, 4, 5, 6};
    State s1(s1arr);          // Creates swaps vector

    // Print out the internal swaps vector
    for (int i = 0; i < s1.swaps[0].size(); ++i) {
        std::cout << s1.swaps[0][i] << "\n";
    }
}