简体   繁体   English

尝试访问 function 调用中的向量元素时出现“错误:太多 arguments 无法运行”

[英]"error: too many arguments to function" when trying to access vector elements in a function call

I'm writing a program to populate a vector from a list of numbers in a file, and then check the list to see how many numbers appear more than once.我正在编写一个程序来从文件中的数字列表中填充一个向量,然后检查该列表以查看有多少数字出现了多次。 The program works when I hard code the array size based on the number of lines in the file.当我根据文件中的行数对数组大小进行硬编码时,该程序可以工作。 But I want to use a vector to automate the sizing of the array for each file since there are many files to process and each is unique in length.但是我想使用向量来自动调整每个文件的数组大小,因为要处理的文件很多,而且每个文件的长度都是唯一的。

I'm confused by this error message because I'm pretty certain I have 3 args in the function declaration and definition.我对此错误消息感到困惑,因为我很确定 function 声明和定义中有 3 个参数。 The error is attached to line 63, and g++ highlights the closing brace of (i) and (j).错误附加到第 63 行,g++ 突出显示 (i) 和 (j) 的右大括号。

  4 #include <iostream>
  5 #include <fstream>
  6 #include <string>
  7 #include <iomanip>
  8 #include <vector>
  9 using namespace std;
 10
 11
 12 void duplicateSort(vector<int> &list, int length, int& fpy);
 13 void printToScreen(int size, int fpy);
 14 void printToFile(int size, int fpy, ofstream &outFile);
 15
 16
 17 int main() {
 18     //declare vars
 19     ifstream inFile;
 20     ofstream outFile;
 21     string serNumber;
 22     //const int size = 8995;
 23     vector<int> list;
 24     int fpy = 0;
 25     string path;
 26
 27     cout << "Enter a file path: " << endl;
 28     cin >> path;
 29
 30     inFile.open(path);
 31     //outFile.open("exubt_2022_fpy.txt");
 32     if (!inFile.is_open()) {
 33         cout << "Bad file path!" << endl;
 34         return 1;
 35     }
 36     list[0] = 0;
 37
 38     //read source file and populate array
 39     while(!inFile.eof())    {
 40         inFile.ignore(200, '_');
 41         inFile.ignore(20, '_');
 42         getline(inFile, serNumber, '.');    //fetch a line of data
 43         int num = stoi(serNumber);          //convert string data to int
 44         inFile.ignore(3);
 45
 46         list.push_back(num);            // add the next number to the back of the vector
 47     }
 48
 49     duplicateSort(list, list.size(), fpy);
 50     printToScreen(list.size(), fpy);
 51     //printToFile(list.size(), fpy, outFile);
 52
 53     inFile.close();
 54     //outFile.close();
 55
 56         return 0;
 57 }
 58
 59 void duplicateSort(vector<int> &list(), int length, int& fpy) {     //sort the list for fpy
 60     int match = 0;
 61     for (int i = 0; i < length; i++) {
 62         for (int j = 0; j < length; j++) {
 63             if(list(i) == list(j))
 64                 match++;
 65             if (match == 1) {
 66                 fpy++;
 67                 match = 0;
 68             }
 69         }
 70     }
 71 }
 72 void printToScreen(int size, int fpy) {
 73     cout << "Total number of units tested = " << size << endl;
 74     cout << "Total number of passed on first try = " << fpy << endl;
 75     cout << "FPY = " << static_cast<double>(fpy) * 100 / size << "%" << endl;
 76 }
 77
 78 void printToFile(int size, int fpy, ofstream &outFile) {
 79     outFile << "Total number of units tested = " << size << endl;
 80     outFile << "Total number of passed on first try = " << fpy << endl;
 81     outFile << "FPY = " << static_cast<double>(fpy) * 100 / size << "%" << endl;
 82 }
 83

In C++, () is a function call operator and is used to call the function.在 C++ 中, ()是 function 调用运算符,用于调用 function。 For example, when you write main() , you are calling the function main which in turn causes the main function to start executing.例如,当您编写main()时,您正在调用 function main ,这反过来又导致main function 开始执行。

To access an element of a vector, you should use subscript operator [] not function call operator () .要访问向量的元素,您应该使用下标运算符[]而不是 function 调用运算符()

I've highlighted the mistakes in your duplicateSort sort function below:我在您的duplicateSort排序 function 中突出显示了错误:

//           function argument should just list the name without `()` operator          
//                             vvvvvvv
void duplicateSort(vector<int> &list(), int length, int& fpy) {     
      //sort the list for fpy
      int match = 0;
      for (int i = 0; i < length; i++) {
          for (int j = 0; j < length; j++) {
//               vvvvvvv    vvvvvvv   to access element of a vector use [] operator
              if(list(i) == list(j))
                  match++;
              if (match == 1) {
                  fpy++;
                  match = 0;
              }
          }
      }
  }

To make your program work, change the function as follows:要使您的程序正常工作,请按如下方式更改 function:

 void duplicateSort(vector<int> &list, int length, int& fpy) {     //sort the list for fpy
      int match = 0;
      for (int i = 0; i < length; i++) {
          for (int j = 0; j < length; j++) {
              if(list[i] == list[j])
                  match++;
              if (match == 1) {
                  fpy++;
                  match = 0;
              }
          }
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM