vector.size() gives an error???

i have used .size() in the past and its worked just fine but here for some reason i'm getting an error. any ideas?


float xPro[3] = {0.0, 0.0, 0.0};
std::cout << "vector: " << xPro.size() << std::endl;


error: request for member ‘size’ in ‘xPro’, which is of non-class type ‘float [3]’

and i have the "include<vector>
xPro is an array, not a vector.
xPro is an array of floats, not a vector of floats.

A vector of floats would look more like
vector<float> xPro;

Please see http://www.cplusplus.com/reference/stl/vector/ for more information on vectors. You can click on the "Constructor" link to see examples of creating vectors.
ok, that makes sense, thank you.

but then, what am i creating if i just say

float xPro[3] = {0, 0, 0}

is that a vector or an array?
and array also has a .size() so why wouldn't it work?
MUST i specify vector or array?
Standard container vector is declared in header <vector>. It si a template class.
To define a vector you should use a constructor. As it was shown above tj define a variable of type vector you should write

std::vector<float> v( 3 );

In this case each element oof the vvector will be initialized by zero 0.0. And its size will be equal to 3.

float xPro[3] = {0, 0, 0};

is definition of an array. Arrays are built-in types. They have no any functions.
But you can calculate the number of alements in it by using expression

sizeof( xPro ) / sizeof( xPro[0] )
thank you for that explanation. very good!
after some thinking i realized it was dumb of me needing to find the size of an array since the size must be defined upon creation, therefore i would already know the size.

but this helped me out too, thanks again!
size of an array sometimes cannot be known when creating an array, like:
1
2
3
4
char arr [] = "this is a simple words using an array";

//to find out the arr size
cout << sizeof (arr) / sizeof (char) << endl;


well, sizeof (char) is actually 1, but this is just an example. you can use a similar calculation to calculate another variable in another type...
Topic archived. No new replies allowed.