I’m a beginner and as an exercise I tried to create a function which gets an array of integers as input and returns their maximum.
Here’s the code:
#include <iostream>
int maximum(int setOfNumbers[]);
int main()
{
int a[] = {1, 2};
std::cout << maximum(a);
return 0;
}
int maximum(int setOfNumbers[])
{
int value = setOfNumbers[0];
for (int n : setOfNumbers)
{
value = std::max(n, value);
}
return value;
}
and this error is what I receive:
demo.cpp: In function 'int maximum(int*)':
demo.cpp:16:18: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
16 | for (int n : setOfNumbers)
| ^~~~~~~~~~~~
| std::begin
In file included from C:/msys64/mingw64/include/c++/13.2.0/string:53,
from C:/msys64/mingw64/include/c++/13.2.0/bits/locale_classes.h:40,
from C:/msys64/mingw64/include/c++/13.2.0/bits/ios_base.h:41,
from C:/msys64/mingw64/include/c++/13.2.0/ios:44,
from C:/msys64/mingw64/include/c++/13.2.0/ostream:40,
from C:/msys64/mingw64/include/c++/13.2.0/iostream:41,
from demo.cpp:1:
C:/msys64/mingw64/include/c++/13.2.0/bits/range_access.h:114:37: note: 'std::begin' declared here
114 | template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
| ^~~~~
demo.cpp:16:18: error: 'end' was not declared in this scope; did you mean 'std::end'?
16 | for (int n : setOfNumbers)
| ^~~~~~~~~~~~
| std::end
C:/msys64/mingw64/include/c++/13.2.0/bits/range_access.h:116:37: note: 'std::end' declared here
116 | template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
| ^~~
Any idea?
PS: I know there is a way of doing this by the normal for-loop, but I’m interested to find out what’s wrong in this foreach-loop and how to fix it.
The Bluehame Owl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.