c++ - 数组下标的无效类型

标签 c++ arrays

我收到了几个数组下标错误,也没有匹配的函数来调用 get line...我不确定到底是为什么。

错误:数组下标的无效类型“int[int]”

错误:数组下标的无效类型“double[int]”

错误:数组下标的无效类型“int[int]”

错误:没有匹配函数来调用“getline(bool)”

到目前为止,这是我的代码:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;

struct CountiesFirst
{
   int counties;
   double poverty_rate;
   double income;
};

const int MAX_COUNTIES = 10;

bool openFile(ifstream& input)
{
  int counties;
  double poverty_rate;
  double income;
  int i=0;
  char filename[256];

  cout << "Welcome to the Appalachian Poverty/Income Report\n";
  cout << "Enter input file name: ";
  cin >> filename;

  input.open(filename);
  getline(input, counties);

  while (getline(!input.eof() && i < MAX_COUNTIES))
  {
     input>>counties[i];
     input>> poverty_rate[i] >> income[i];
     ++i;
  }
  while (input.eof())
  {
     input.putback (i) ;
     cout << "Input file is empty" << endl;
     return i;
  }

  return !input.fail();
  }

  void printTable(string counties[],
      double poverty_rate[],
      double income[],
      size_t size)
  {
  cout << "Counties     Poverty Rate        Salary\n";
  cout << "---------------------------------------\n";
  for (size_t i = 0; i < size; ++i)
  {
    cout.width(17);
    cout << left << counties[i];
    cout << poverty_rate[i] << "%";
    cout.width(13);
    cout << right << "$" << income[i] << endl;
  }
}


  int main()
  {
    CountiesFirst counties [MAX_COUNTIES];
    int filename;

    ifstream input;
    ofstream output;

    if (openFile(input))
   {
  //sort(counties,  poverty_rate, income, size);
  void printTable(string counties[], double poverty_rate[],  double income[],       int size);
  }
  else
  {
     cout << "Input file does not exist.\n";
     exit(EXIT_FAILURE);

     input.close();
     output.close();
   }
  return 0;
 }

最佳答案

  1. 这里:

    int counties;
    double poverty_rate;   
    
    input >> counties[i];
    input >> poverty_rate[i] >> income[i];
    

    您正在尝试使用 intdouble,就像它们是数组一样。他们不是。
    它会产生“数组下标的无效类型”错误。

  2. getlineistreamstring 作为参数。

    同时,这里你传递int:

    int counties;
    getline(input, counties);
    

    这里你做的事情是完全错误的:

    getline(!input.eof() && i < MAX_COUNTIES)
    

    它会产生“没有匹配的函数”错误。

关于c++ - 数组下标的无效类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034811/

相关文章:

java - 将文件中的单词存储到字符串 Java

c++ - 如果未分配返回值,是否需要删除或释放动态数组?还是被函数删除了?

c++ - ZeroMQ socket.recv() 引发了 STACK_OVERFLOW 异常

c++ - 为什么 C++ 数组的括号在变量上而不是在数据类型上?

java - 将 JTextField 文本存储到数组中并求和 - Java

Android:如何在播放媒体(mp3)时以一定毫秒显示文本

c++ - Direct2D 实际上可以与 direct3D 11 设备一起使用吗?

c++ - 旧发行版上的二进制兼容性(使用 C++11)

c++ - 折叠表达式 : iterate over variadic template type parameter to check compile-time conditions on the comprising types

ios - 保持 TableView 之间的数组顺序?