Tried for nearly 3 hours to find out whats wrong with my code now. It's really making me tear my hair out. Like, the last time im reading from Accelerated C++ im in Chapter 4 started c++, as a whole a month ago. I fixed my last problem by adding the files ,but now theres a new problem. There is this error that keeps popping up saying:
undefined reference to 'read(std::istream&Student_info&)'
undefined reference to 'compare(Student_info const&, Student_info const&)
undefined reference to 'grade(Student_info const&)'
This all of the code its really long.
Main:
#include "Student_info.h"
using std::istream;
using std::vector;
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
istream& read(istream& is, Student_info& s)
{
is >> s.name >> s.midterm >> s.final;
read_hw(is,s.homework); //read and store all the students' homework grades
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if(in)
{
//get rid of previous contents
hw.clear();
//read homework grades
double x;
while(in>>x)
hw.push_back(x);
//clear the stream so that the input would work for the next student
in.clear();
}
return in;
}
But you only have a prototype for this function (in mediah.h) and you never actually defined this function.
I have defined the median function for you.
Note: THIS FUNCTION ASSUMES THAT THE VECTOR IS SORTED. IF THE VECTOR OF DOUBLES IS NOT SORTED FROM LOWEST TO HIGHEST, THIS WILL NOT WORK.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
double median(std::vector<double> VectorOfGrades)
{
if (VectorOfGrades.size() == 0) //If there are no grades in the vector
return 0.0;
if (VectorOfGrades.size() % 2 == 0) //If there is an even # of grades
{
int Index = VectorOfGrades.size() / 2;
double SumOfMiddleGrades = VectorOfGrades[Index] + VectorOfGrades[Index - 1]; //Add two middle grades together
SumOfMiddleGrades /= 2; //Divide the sum by 2 to get the average for the median
return SumOfMiddleGrades; //Return calculated median
}
else //If there is an odd # of grades
{
int Index = VectorOfGrades.size() / 2;
return VectorOfGrades[Index];
}
}