Hi guys,
I'm new in this awesome language and I recently tried to make a simple function which could count how many elements are there in a given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream> //std::cout std::cin
usingnamespace std;
size_t eleArray(unsignedshort v[])
{
size_t numele = 0;
for (short ele : v) numele ++; //<----- I GET SOME ERRORS ON THIS LINE
return numele;
}
int main() {
constshort ARRAY = 5;
unsignedshort v[ARRAY] { 1,2,3,4,5 };
short n = eleArray(v);
if (ARRAY == n) cout << "ok";
getchar();
return 0;
}
For some reason my compiler (I'm using Visual Studio 2019) returns some errors, but I still cannot fix them.
Here a list of the errors (they are all referring to the same line):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Error (active) E2261 6 this range-based 'for' statement requires a suitable "begin" function and none was found
Error C2672 6 'begin': no matching overloaded function found
Error C2863 6 Failed to specialize function template'unknown-type std::begin(_Container &)'
Error C2784 6 'const _Elem *std::begin(std::initializer_list<_Elem>) noexcept': could not deduce template argument for'std::initializer_list<_Elem>' from 'unsigned short []'
Error C2672 6 'end': no matching overloaded function found
Error C2863 6 Failed to specialize function template'unknown-type std::end(_Container &)'
Error C2784 6 'const _Elem *std::end(std::initializer_list<_Elem>) noexcept': could not deduce template argument for'std::initializer_list<_Elem>' from 'unsigned short []'
Error C3536 6 '<begin>$L0': cannot be used before it is initialized
Error C3536 6 '<end>$L0': cannot be used before it is initialized
Error C2100 6 illegal indirection
An array degrades into a pointer when you pass it to a function.
In other words, the size of the array isn't known.
The easiest way to fix this is to pass the number of elements of the array into the function as second parameter. You won't be able to use a for-each loop.
Isn't there another way to keep that piece of information, maybe by a value parameter instead of the default reference parameter given to any array?
Sure, you can pass it by value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
size_t eleArray(unsignedshort v[], int size_of_the_array) // pass size_of_the_array by value
{
return size_of_the_array;
}
int main() {
constshort ARRAY = 5;
unsignedshort v[ARRAY] { 1,2,3,4,5 };
short n = eleArray(v, ARRAY);
if (ARRAY == n) cout << "ok";
getchar();
return 0;
}
Although as a beginner you shouldn't really be using arrays. You should be using a vector. vector is for beginners. And everyone else as well. Arrays are for when you have no choice. A vector knows its own size.
What the function receives is just a pointer. That's it. That's all. The other information - the size of the array - does not exist in that function.
If you want to be able to pass an array that still carries that information, use a vector. It's why they exist.
if I'm going to use eleArray() in other source codes
Don't.
Use a vector. It's what they're for. If you don't want to use C++, that's fine, but that's your choice. If you want an array that holds its size information when you pass them to a function, that's a vector. The solution to your problem is to use a vector; do not use arrays. Do not use arrays. Do not use arrays.