I started learning C++ two days ago and this error I am getting is obscure to me, I am trying to do the following
int sumArray(const int arr)
{
int sum = 0;
for (auto &n : arr) {
sum += n;
}
return sum;
};
int main ()
{
int numbers[] = {1, 2, 5, 10};
return sumArray(numbers);
}
which is a slight change from an example in "A Tour of C++", the error I am getting is
cpprepl.cpp: In function ‘int sumArray(int)’:
cpprepl.cpp:4:18: error: ‘begin’ was not declared in this scope
for (auto &n : arr) {
^~~
cpprepl.cpp:4:18: error: ‘end’ was not declared in this scope
cpprepl.cpp: In function ‘int main()’:
cpprepl.cpp:13:26: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
return sumArray(numbers);
^
cpprepl.cpp:1:5: note: initializing argument 1 of ‘int sumArray(int)’
int sumArray(const int arr)
^~~~~~~~
If I do
int main () {
int arr[] = {1, 2, 5, 10};
int sum = 0;
for (auto &n : arr) {
sum += n;
}
return sum;
}
everything is fine, so I suspect I am not understanding pointers and how C++ is passing numbers
to sumArray
; I've seen multiple questions on similar subjects but I'm still missing how this should be done.