1

I'm getting the error "no matching function for call to readProgrammers" how can I fix this?

this is the main.cpp file I also have a .h file the declares all the functions

#include <iostream>
#include <string>
#include <fstream>
#include "programmer.h"

using namespace std;

const int MAX_PROGRAMMERS = 10;

int main()
{
    Programmer programmers[MAX_PROGRAMMERS];
    int numProgrammers = 0;
   
    ifstream inputFile( "programmers.txt" ); // declares file stream and opens for input
  
    if( inputFile.good() )
    {
        numProgrammers = readProgrammers( inputFile, programmers, MAX_PROGRAMMERS );
      
        //close the input file because we are done with it
        inputFile.close();
      
        //report on programmers to console
        generateProgrammerReport( cout, programmers, numProgrammers ); // cout passed as an ostream
      
        //report on programmers to a file
        ofstream outputFile("ProgrammerReport.txt");
        generateProgrammerReport( outputFile, programmers, numProgrammers );
        outputFile.close();
    }
    else
    {
        cout << "File not opened properly!!\n\n";
    }
   
    system("pause");
    return 0;
   
}

this is the programmer.cpp file. This is the file that has the error in the generateProgrammerInfo function when I am trying to call the function readProgrammers.

#include "programmer.h"
#include "sstream"
#include "iomanip"

//implement functions here!

//returns the number of programmers - fills in the programmers array from the ifstream
int readProgrammers( ifstream& inputFile, Programmer programmers[], int maxProgrammers )
{
  int numProgrammers = 0;
  int programmer_id;
  string name;
  int lines;
  if(inputFile.is_open())
  {
    while (!inputFile.eof() && numProgrammers < maxProgrammers)
    {
      inputFile >> programmers[numProgrammers].programmer_id;
      inputFile >> programmers[numProgrammers].name;
      inputFile >> programmers[numProgrammers].lines;
      numProgrammers++;
    }
  }
    //just a stub!
    return numProgrammers;
}

//returns the total of the lines field for all programmers
int calcTotalLines( Programmer programmers[], int numProgrammers )
{
  int totalLine = 0;
  for (int i = 0; i < numProgrammers; i++)
  {
    totalLine += programmers[i].lines; 
  }
  
    //just a stub!
    return totalLine;
}

//returns the average lines coded for all programmers as a float
float calcAverageLines( Programmer programmers[], int numProgrammers )
{
  int totalLine = calcTotalLines(programmers, numProgrammers);
  int averageLines = 0;
  averageLines = totalLine/numProgrammers;
    //just a stub!
    return averageLines;
}
//return a string containing info for a particular programmer
string generateProgrammerInfo( Programmer theProgrammer )
{
  ifstream inputFile;
  Programmer programmers;
  int MAX_PROGRAMMERS;
  readProgrammers( inputFile, programmers, MAX_PROGRAMMERS );
  int numProgrammers;
  int programmer_id;
  string name;
  int lines;
  const int SPACING = 5;
  stringstream sout;
  sout << setw(SPACING) << theProgrammer.programmer_id << " " << theProgrammer.name << " " << theProgrammer.lines << endl;
    //just a stub!
    return sout.str();
}

//generate report for all programmers
//call calcTotalLines, calcAverageLines, and generateProgrammerInfo functions
void generateProgrammerReport( ostream& output, Programmer programmers[], int numProgrammers )
{
  Programmer theProgrammer;
  generateProgrammerInfo(theProgrammer);
  calcTotalLines(programmers, numProgrammers);
  calcAverageLines(programmers, numProgrammers);
    //just a stub!
    output << "These are all the programmers:" << endl;
    for( int i = 0; i < numProgrammers; i++ )
    {
        output << generateProgrammerInfo(theProgrammer); //this should output the programmer info for each programmer
    }
    output << "\nTotal lines = " << calcTotalLines(programmers, numProgrammers); //this should ouptut the total lines
    output << "\nAverage lines = " << calcAverageLines(programmers, numProgrammers); //this should output the average lines
    output << endl;
}

This is the programmer.h file

#ifndef __lab2__programmer__
#define __lab2__programmer__

#include <string>
#include <fstream>

using namespace std;

struct Programmer
{
    int programmer_id;
    string name;
    int lines; // lines of code programmed
};

//function prototypes

//returns the number of programmers - fills in the programmers array from the ifstream
int readProgrammers( ifstream& inputFile, Programmer programmers[], int maxProgrammers );

//returns the total of the lines field for all programmers
int calcTotalLines( Programmer programmers[], int numProgrammers );

//returns the average lines coded for all programmers as a float
float calcAverageLines( Programmer programmers[], int numProgrammers );

//return a string containing info for a particular programmer
string generateProgrammerInfo( Programmer theProgrammer );

//generate report for all employees
void generateProgrammerReport( ostream& output, Programmer programmers[], int numProgrammers );


#endif /* defined(__lab2__programmer__) */

I can't change the main.cpp file or the programmer.h file.

  • yes it is declared –  Jan 31 '22 at 21:14
  • [can't reproduce](https://godbolt.org/z/9Td8xKbej), please show a [mre] including the full error message from the compiler – Alan Birtles Jan 31 '22 at 21:18
  • 1
    The error message most likely continues with a list of the candidates the compiler has indentified. What are they? – molbdnilo Jan 31 '22 at 21:18
  • also note that identifiers starting with 2 underscores are reserved for use by the compiler. `using namespace std` [should be avoided](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and should never be used in a header file – Alan Birtles Jan 31 '22 at 21:20
  • all the error message says is "no matching function for call to readProgrammers". I think that I'm calling the function wrong or something but I'm not sure. –  Jan 31 '22 at 21:27
  • 2
    That’s guaranteed to not be all the error message says. – sweenish Jan 31 '22 at 21:29
  • Do you have to use C-style arrays? Otherwise consider using `std::array` – infinitezero Jan 31 '22 at 21:31
  • it also says "Candidate function not viable: no known conversion from 'Programmer' to 'Programmer *' for 2nd argument; take the address of the argument with &" –  Jan 31 '22 at 21:32
  • OK, this is why we ask for a [mre], `main.cpp` is not relevant to the question and could be omitted. In `generateProgrammerInfo` you are doing `readProgrammers( inputFile, programmers, MAX_PROGRAMMERS );` but `programmers` is an object not an array so doesn't match the function arguments, the error message seems fairly self explanatory? – Alan Birtles Jan 31 '22 at 21:53

1 Answers1

1

In generateProgrammerInfo(), this code is wrong:

ifstream inputFile;
Programmer programmers;
int MAX_PROGRAMMERS;
readProgrammers( inputFile, programmers, MAX_PROGRAMMERS );

First off, inputFile in a local variable that does not have a file opened, so there is nothing for readProgrammers() to read. But more importantly, readProgrammers() expects a pointer to an array of Programmer objects, but you are passing in a single Programmer object. That is why the compiler is complaining that it can't convert a Programmer to a Programmer*. Also, readProgrammers() expects the number of Programmer objects in the array, but MAX_PROGRAMMERS is an uninitialized local variable.

If your goal were to read in a single Programmer object, then you can use this:

string generateProgrammerInfo( Programmer &theProgrammer )
{
  ...
  readProgrammers( inputFile, &theProgrammer, 1 );
  ...
}

However, given how generateProgrammerInfo() is being used inside of generateProgrammerReport(), this wold still be wrong. generateProgrammerInfo() should not be trying to read in anything at all, only output things. And generateProgrammerReport() is coded wrong, too. It is taking in an array of Programmer objects, but is not printing any of them out, it is instead trying to read into a local Programmer variable and then printing that out instead.

There are many other issues with your code, too.

  • using "" instead of <> for standard headers
  • using !eof() in a loop incorrectly.
  • calcAverageLines() performing integer division instead of floating-point division.
  • generateProgrammerReport() calling calcTotalLines() and calcAverageLines() redundantly.

With that said, try this instead:

#include "programmer.h"
#include <sstream>
#include <iomanip>

//returns the number of programmers - fills in the programmers array from the ifstream
int readProgrammers( ifstream& inputFile, Programmer programmers[], int maxProgrammers )
{
    int numProgrammers = 0;
    int programmer_id;
    string name;
    int lines;
    while ((numProgrammers < maxProgrammers)
        && (inputFile >> programmer_id)
        && (inputFile >> name)
        && (inputFile >> lines)
    {
        programmers[numProgrammers].programmer_id = programmer_id;
        programmers[numProgrammers].name = name;
        programmers[numProgrammers].lines = lines;
        ++numProgrammers;
    }
    return numProgrammers;
}

//returns the total of the lines field for all programmers
int calcTotalLines( Programmer programmers[], int numProgrammers )
{
    int totalLine = 0;
    for (int i = 0; i < numProgrammers; ++i)
    {
        totalLine += programmers[i].lines; 
    }
    return totalLine;
}

//returns the average lines coded for all programmers as a float
float calcAverageLines( Programmer programmers[], int numProgrammers )
{
    float totalLine = calcTotalLines(programmers, numProgrammers);
    return totalLine / numProgrammers;
}

//return a string containing info for a particular programmer
string generateProgrammerInfo( Programmer theProgrammer )
{
    const int SPACING = 5;
    stringstream sout;
    sout << setw(SPACING) << theProgrammer.programmer_id << " " << theProgrammer.name << " " << theProgrammer.lines << endl;
    return sout.str();
}

//generate report for all programmers
//call calcTotalLines, calcAverageLines, and generateProgrammerInfo functions
void generateProgrammerReport( ostream& output, Programmer programmers[], int numProgrammers )
{
    output << "These are all the programmers:" << endl;
    for( int i = 0; i < numProgrammers; ++i )
    {
        output << generateProgrammerInfo( programmers[i] ); //this should output the programmer info for each programmer
    }
    output << "\nTotal lines = " << calcTotalLines(programmers, numProgrammers); //this should output the total lines
    output << "\nAverage lines = " << calcAverageLines(programmers, numProgrammers); //this should output the average lines
    output << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770