C++ code with header files giving out function errors which is undefined [closed]-Collection of common programming errors
#ifndef GUARD_student_info_h
#define GUARD_student_info_h
#include
#include
#include
struct student_info
{
std::string name;
double midterm, final;
std::vectorhomework;
};
bool compare(const student_info&, const student_info&);
std::istream& read(std::istream&, student_info&);
std::istream& read_hw(std::istream&, std::vector&);
#endif
This is the student_info.h header file
#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);
return is;
}
istream& read_hw(istream& in, vector& hw)
{
if(in)
{
hw.clear();
double x;
while(in >> x)
hw.push_back(x);
in.clear();
}
return in;
}
This is student_info.cpp
#ifndef GUARD_median_h
#define GUARD_median_h
#include
double median(std::vector);
#endif
This is median.h
#include
#include
#include
using namespace std;
using std::domain_error;
using std::vector;
double median(vectorvec) // No need of saying it as vectorhomework, cos we wont be using this to always give median of hw, but median of some other data
{
typedef vector::size_type vec_sz;
vec_sz size;
size = vec.size();
if(size == 0)
throw domain_error("Empty homework. From medain.cpp when vec.size() == 0.");
sort(vec.begin(), vec.end());
vec_sz mid = size / 2;
return size % 2 == 0 ? vec[mid] + vec[mid - 1] / 2 : vec[mid];
}
This is median.cpp
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include
#include "student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector&);
double grade(const student_info&);
#endif
This is grade.h
#include
#include
#include "grade.h"
#include "median.h"
#include "student_info.h"
using namespace std;
using std::domain_error;
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
double grade(double midterm, double final, const vector& hw)
{
if(hw.size() == 0)
throw domain_error("No homework. From grades function 2 in grade.cpp.");
grade(midterm, final, median(hw));
}
double grade(const student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
This is grade.cpp
#include
#include
#include
#include
#include
#include
#include
#include "grade.h"
#include "student_info.h"
using namespace std;
using std::streamsize;
using std::setprecision;
using std::domain_error;
using std::max;
istream& read(istream& , student_info &);
int main()
{
vectorstudents;
student_info record;
string::size_type maxlen = 0;
while(read(cin, record))
{
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare);
for (vector::size_type i = 0;i != students.size(); ++i) {
// write the name, padded on the right to maxlen + 1 characters
cout
Originally posted 2013-11-27 11:53:21.