我开始学习C++前两天这个错误,我得到的是模糊的我,我尝试做以下汽车在此范围内未声明
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);
}
这是从 “A巡回赛C++的” 一个例子略有变化,我得到的错误是
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) ^~~~~~~~
如果我做
int main() {
int arr[] = {1, 2, 5, 10};
int sum = 0;
for (auto &n : arr) {
sum += n;
}
return sum;
}
一切都很好,所以我怀疑我不了解指针和C++如何通过numbers
到sumArray
;我在类似的主题上看到了很多问题,但我仍然想不到应该怎么做。
S /'INT sumArray(const int的ARR)'/'INT sumArray(const int的改编[4])' – user0042
@ user0042这样做,仍然获得约'begin'前两个错误, 'end'。 –
我建议你应该使用'std :: vector'而不是原始数组。 –
user0042