Search code examples
c++arraysvectortypedef

vector of double[2] error


why this error:

#include <vector>
typedef double point[2];

int main()
{
     std::vector<point> x;
}
/usr/include/c++/4.3/bits/stl_construct.h: In function ‘void std::_Destroy(_Tp*) [with _Tp = double [2]]’:
/usr/include/c++/4.3/bits/stl_construct.h:103:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = double (*)[2]]’
/usr/include/c++/4.3/bits/stl_construct.h:128:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator&) [with _ForwardIterator = double (*)[2], _Tp = double [2]]’
/usr/include/c++/4.3/bits/stl_vector.h:300:   instantiated from ‘std::vector::~vector() [with _Tp = double [2], _Alloc = std::allocator]’
prova.cpp:8:   instantiated from here
/usr/include/c++/4.3/bits/stl_construct.h:88: error: request for member ‘~double [2]’ in ‘* __pointer’, which is of non-class type ‘double [2]’

how to solve?


Solution

  • You can't do that. As mentioned, arrays are not copyable or assignable which are requirements for std::vector. I would recommend this:

    #include <vector>
    struct point {
        double x;
        double y;
    };
    
    int main() {
         std::vector<point> v;
    }
    

    It will read better anyway since you can do things like:

    put(v[0].x, v[0].y, value);
    

    which makes it more obvious this the vector contains points (coordinates?)