c++ - 'int accidents[5]' 的声明隐藏参数错误

标签 c++ function parameters shadow

我是 C++ 的新手,找不到任何解决此错误的方法。据我所知,我没有重载任何变量来创建与命名的冲突(这是我在网上可以找到的最接近原因的事情)。任何帮助将不胜感激。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int getNumAccidents(string);
int findLowest(int accidents[]);
int main()
{
 const int ARRAY_SIZE = 5;
 int input;
 int accidents[ARRAY_SIZE];
 int counter = 0;
 string regions[ARRAY_SIZE] = {"North", "South", "East", "West", "Central"};
 string name;
 string lowestRegion;
 int lowest = 1000000000;
 while (counter < ARRAY_SIZE)
 {
  name == regions[counter];
  accidents[counter] = getNumAccidents(name);
  counter++;
 }
 findLowest(accidents);
 system("PAUSE");
 return 0;
}

int getNumAccidents(string name)
{
 int input;
 validate:
 cout<<"Enter the number of accidents that took place in the "<<name<<" region in the                  last year:"<<endl;
 cin>>input;
 if (input < 0)
 {
  system("CLS");
  cout<<"Invalid input: the number of accidents can not be negative.";
  goto validate;
 }
 return input;
}

int findLowest(int accidents[])
{
 const int ARRAY_SIZE = 5;
 string lowestRegion;
 int accidents[ARRAY_SIZE];
 string regions[ARRAY_SIZE] = {"North", "South", "East", "West", "Central"};
 int lowest = 0;
 int counter = 0;
 while (counter < ARRAY_SIZE)
 {
  if (accidents[counter] < lowest)
  {
   lowest = accidents[counter];
   lowestRegion = regions[counter];
  }
  counter++;
 }
 cout<<"The "<<lowestRegion<<" region had the least accidents in the past year at " <<lowest<<" accidents."<<endl;
}

最佳答案

        int findLowest(int accidents[])
    //                      ^^
        {
         const int ARRAY_SIZE = 5;
         string lowestRegion;
         int accidents[ARRAY_SIZE];
 //               ^^

你的 findLowest 中有两个 accidents

关于c++ - 'int accidents[5]' 的声明隐藏参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266015/

相关文章:

matlab - 在MATLAB中具有可变数量参数的SQLite语句

c++ - Linux kill() 意外错误

c++ - 函数返回类型样式

c++ - 从返回 int c++ 的函数中获取二维数组

javascript - 尝试完成这个闭包示例...?

swift - 如何将 json 数据发送到 API swift

c++ - 哪种数据结构最适合 C++ 中的基本堆栈和队列?

c++ - 万无一失的配置方式#define/#ifdef

php - 动态日期选择(Php/JS)

python - def some_method(param : int) syntax?) 有什么意义