Could someone tell what is the correct way to work with a vector of arrays?
I declared a vector of arrays (vector<float[4]>
) but got error: conversion from 'int' to non-scalar type 'float [4]' requested
when trying to resize
it. What is going wrong?
Use:
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.You can, however, use an
array
class template, like the one provided by Boost, TR1, and C++0x:(You'll want to replace
std::array
withstd::tr1::array
to use the template included in C++ TR1, orboost::array
to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)There is no error in the following piece of code:
OUTPUT IS:
6.28
2.5
9.73
4.364
IN CONCLUSION:
is another possibility apart from
that James McNellis suggested.
Every element of your vector is a
float[4]
, so when you resize every element needs to default initialized from afloat[4]
. I take it you tried to initialize with anint
value like0
?Try: