In the intermediate lesson “3- Determining the Size of Arrays” Mosh introduces the size() function to determine the size of an array. However, it doesn’t work on my system (macOS, CLion, C++20).
Does anybody else have that problem?
It does work though if I define an arrays differently:
array<int, 3> my_array;
cout << my_array.size();
I also read that it is better to use vectors than arrays. But I think vectors are not included in this course.
size function is called as
std::size(my_array) if not in namespace std). It is not a method on some array class. You also need to make sure you included the appropriate header files to get access to the
As I said, it is not working. See attached screenshots.
Sorry you are mixing two different things. There is the struct
std::array that you are importing and using in the second screenshot which is different than the primitive arrays. Your first screenshot shows the primitive array called
To make the first one work, I think you just need the correct header which I think is iterator:
#include <iterator> (would have to double check the documentation to be sure that was the correct header).
Thanks. Including iterator doesn’t work.
But it works in XCode — see attached. Must be something in CLion?
I don’t know about struct array. I found that code when I googled for a solution.
I found the problem. It seems I was not running C++20 in CLion. When I changed the following line in CMakeLists.txt from
Glad you were able to work it out!