Home > front end >  "error: too many arguments to function" when trying to access vector elements in a functio
"error: too many arguments to function" when trying to access vector elements in a functio

Time:09-12

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. The error is attached to line 63, and g highlights the closing brace of (i) and (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

CodePudding user response:

In C , () is a function call operator and is used to call the function. For example, when you write main(), you are calling the function main which in turn causes the main function to start executing.

To access an element of a vector, you should use subscript operator [] not function call operator ().

I've highlighted the mistakes in your duplicateSort sort function below:

//           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:

 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;
              }
          }
      }
  }
  • Related