Error in a C++ function

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
using namespace std;
size_t eleArray(unsigned short v[])
{
size_t numele = 0;
for (short ele : v) numele ++; //<----- I GET SOME ERRORS ON THIS LINE
return numele;
}
int main() {
const short ARRAY = 5;
unsigned short 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.
Last edited on
The goal of this function is exactly to find the size of the array, so it doesn't make sense if you declare it as a parameter...

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?
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>
using namespace std;

size_t eleArray(unsigned short v[], int size_of_the_array) // pass size_of_the_array by value
{
  return size_of_the_array;
}

int main() {
  const short ARRAY = 5;
  unsigned short 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#include <iostream>
using namespace std;


int function(vector<short> v)
{
	return v.size();
}

int main() 
{
  vector<short> vec { 1,2,3,4,5 };
  cout << function(vec);
}


Last edited on
@Repeater thank you, but could I get that constant without knowing it?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> //std::cout std::cin
using namespace std;
int main() {
const short ARRAY = 5;
unsigned short v[ARRAY] { 1,2,3,4,5 };
size_t numele = 0;
for (auto it : v) numele ++;
if (ARRAY == numele) cout << "ok";
getchar();
return 0;
}


It works in main(), so I hope I can do the same thing in a function.

What I mean is that I won't know the value of ARRAY constant if I'm going to use eleArray() in other source codes, so I want to obtain it!
Where this is going is no. You cannot.

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.
Last edited on
Ok, thank you very much... I didn't know what a vector is so I was using classic C arrays.

Problem solved :)
You can also pass the array by reference.

https://stackoverflow.com/questions/5724171/passing-an-array-by-reference/5724184

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

template<int N>
int foo(int (&myArray)[N])
{
    return N;
}

int main()
{
    int a[100];
    std::cout << foo(a);
}
Topic archived. No new replies allowed.