Reputation: 77
I'm working on an assignment where we have to create a header and source file to be used in a pre-existing main source file. I have some functions that are supposed to be called, but the program says that the functions don't match.
My main problem is that in the main code, it says that there are "no matching function for call to" both Student::addGrade and Student::computeGPA. I was able to get this program to compile earlier by removing the "const Student& aStudent); from the declarations of computeGPA and addGrade (in my .h file), and have the variables in the actual definition of the functions (the ones in student.cpp) be simply "qp" or "credits" instead of "aStudent.qp" or "aStudent.credits". My program was able to compile, but it gave me a clearly wrong output (a number of around 40 digits or so, as opposed to a name and a number with two decimal places). I do not understand why my program says that the function doesn't match to the call, and I don't know where to go from here. Any help would be appreciated, thank you.
The preexisting code that calls my header and source files is
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include "student.h"
#include "gradeValue.h"
using namespace std;
void gpaCalc (istream& input, ostream& output)
{
string name;
getline (input, name);
int credits;
double qp;
input >> credits >> qp;
Student student(name, credits, qp);
string grade;
input >> grade >> credits;
while (grade != "0" && credits != 0)
{
student.addGrade (grade, credits);
input >> grade >> credits;
}
double gpa = student.computeGPA();
output << student.name << ' '
<< fixed << setprecision(2) << gpa << endl;
}
int main (int argc, char** argv)
{
if (argc > 1)
{
ifstream inputFile (argv[1]);
gpaCalc (inputFile, cout);
}
else
gpaCalc (cin, cout);
return 0;
}
Here's the files I've written myself, student.h and student.cpp
student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <string>
struct Student
{
Student(std::string name, int credits, double qp);
std::string name;
int credits;
double qp;
double computeGPA (const Student& aStudent);
void addGrade(const Student& aStudent, std::string grade, int credits);
};
#endif // STUDENT_H_INCLUDED
student.cpp
#include "student.h"
using namespace std;
struct GradeValue {
string grade;
double value;
};
const GradeValue gradeValues[] = {
{"A", 4.0},
{"A-", 3.7},
{"B+", 3.3},
{"B", 3.0},
{"B-", 2.7},
{"C+", 2.3},
{"C", 2.0},
{"C-", 1.7},
{"D+", 1.3},
{"D", 1.0},
{"F", 0.0}
};
double findGradeValue (const GradeValue table[], int tableSize, string grade)
{
for (int i = 0; i < tableSize; ++i)
if (table[i].grade == grade)
return table[i].value;
return table[tableSize-1].value;
}
void Student::addGrade(Student& aStudent, string grade, int credits)
{
aStudent.credits += credits;
double v = findGradeValue(gradeValues, 11, grade);
aStudent.qp += credits * v;
}
double Student::computeGPA(const Student& aStudent)
{
if (aStudent.credits > 0)
return aStudent.qp / aStudent.credits;
else
return 0.0;
}
Student::Student(std::string name, int credits, double qp) {};
Upvotes: 0
Views: 2777
Reputation: 4387
Your definitions for your Student class' functions are strange, and this strangeness I believe caused you to implement them incorrectly as well as call them incorrectly. When you have a member function, you don't need to pass it itself, so you can omit that part. These lines in student.h
:
double computeGPA (const Student& aStudent);
void addGrade(const Student& aStudent, std::string grade, int credits);
Should likely be:
double computeGPA ();
void addGrade(std::string grade, int credits);
Then, in the implementation of them, you should replace aStudent.
with this->
. You have the following (note that addGrade is missing const
on its Student
parameter, but it doesn't matter cause we're gonna delete it):
void Student::addGrade(Student& aStudent, string grade, int credits) {
aStudent.credits += credits;
double v = findGradeValue(gradeValues, 11, grade);
aStudent.qp += credits * v;
}
double Student::computeGPA(const Student& student) {
if (credits > 0)
return aStudent.qp / aStudent.credits;
else
return 0.0;
}
consider changing them to:
void Student::addGrade(string grade, int credits) {
this->credits += credits;
double v = findGradeValue(gradeValues, 11, grade);
this->qp += credits * v;
}
double Student::computeGPA() {
if (credits > 0)
return this->qp / this->credits;
else
return 0.0;
}
Upvotes: 0
Reputation: 490038
Where you call computeGPA
, you don't pass any parameters:
double gpa = student.computeGPA();
...but in the header, where you've declared Student::computeGPA()
, you specify it as taking a parameter:
double computeGPA (const Student& aStudent);
...therefore, the call and the function don't match, and the compiler tells you it can't find a function that matches a call passing no parameters.
Likewise, where you call addGrade
you pass two parameters, but the declaration says it expects three.
At a guess, you probably want to eliminate the Student &
parameters--member functions will normally act on the object on which you invoked them.
Upvotes: 1