Вопрос о хранении массива в std::vector в С++C++

Программы на C++. Форум разработчиков
Anonymous
 Вопрос о хранении массива в std::vector в С++

Сообщение Anonymous »

Мне неясны следующие моменты.

Во-первых, этот код компилируется нормально:

#include

typedef struct{
int x1,x2,x3,x4;
} ints;

typedef std::vector vec;

int main(){
vec v;
ints a = {0,1,2,3};
v.push_back(a);
}


Следующий код практически идентичен:

#include

typedef std::vector vec;

int main(){
vec v;
int a[4] = {0,1,2,3};
v.push_back(a);
}


но он выдает очень длинный вывод ошибки, который я включу в конец. Почему компилятор обращается с этими двумя программами по-разному? Это определенно не интуитивно понятно.

Вот ошибка компилятора, возникающая в моей системе, использующей g++ для компиляции:

[mattg@pigott Test]$ g++ test2.cpp -o test2
In file included from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/i586-redhat-linux/bits/c++allocator.h:34,
from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/allocator.h:48,
from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/vector:62,
from test2.cpp:2:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator::construct(_Tp*, const _Tp&) [with _Tp = int [4]]’:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_vector.h:737: instantiated from ‘void std::vector::push_back(const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’
test2.cpp:9: instantiated from here
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h:105: error: ISO C++ forbids initialization in array new
In file included from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/vector:69,
from test2.cpp:2:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc: In member function ‘void std::vector::_M_insert_aux(__gnu_cxx::__normal_iterator, const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_vector.h:741: instantiated from ‘void std::vector::push_back(const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’
test2.cpp:9: instantiated from here
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc:306: error: array must be initialized with a brace-enclosed initializer
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_vector.h:741: instantiated from ‘void std::vector::push_back(const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’
test2.cpp:9: instantiated from here
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc:312: error: invalid array assignment
In file included from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/i586-redhat-linux/bits/c++allocator.h:34,
from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/allocator.h:48,
from /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/vector:62,
from test2.cpp:2:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator::destroy(_Tp*) [with _Tp = int [4]]’:
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc:353: instantiated from ‘void std::vector::_M_insert_aux(__gnu_cxx::__normal_iterator, const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_vector.h:741: instantiated from ‘void std::vector::push_back(const _Tp&) [with _Tp = int [4], _Alloc = std::allocator]’
test2.cpp:9: instantiated from here
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h:115: error: request for member ‘~int [4]’ in ‘* __p’, which is of non-class type ‘int [4]’


Подробнее здесь: https://stackoverflow.com/questions/146 ... ector-in-c

Вернуться в «C++»