New error I can't find out :(

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include "grade.h"
#include "Student_info.h"
using std::cout;
using std::cin;
using std::domain_error;
using std::endl;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;

int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;

    while (read(cin, record)) {
        maxlen = max(maxlen, record.name.size());
        students.push_back(record);
        cout << "entered";
        cout << students[0].name << string(maxlen+1 - students[0].name.size(), ' ');
    }
    sort(students.begin(), students.end(), compare);

    for(vector<Student_info>::size_type i=0;
        i != students.size(); ++i) {
            cout << students[i].name << string(maxlen+1 - students[i].name.size(), ' ');

            try {
                double final_grade = grade(students[i]);
                streamsize prec = cout.precision();
                cout << setprecision(3) << final_grade << setprecision(prec);
            } catch (domain_error e) {
                cout << e.what();
            }
            cout << endl;
    }
    cin.ignore();
    cin.get();
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
#ifndef GRADE_H_INCLUDED
#define GRADE_H_INCLUDED

// grade.h
#include <vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);

#endif // GRADE_H_INCLUDED 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef GRADE_CPP_INCLUDED
#define GRADE_CPP_INCLUDED

#include <stdexcept>
#include <vector>
#include "grade.h"
#include "mediah.h"
#include "Student_info.h"
using std::domain_error;
using std::vector;
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<double>& hw)
{
       if (hw.size() == 0)
           throw domain_error("student has done no homework");
       return grade(midterm, final, median(hw));
}
double grade(const Student_info& s)
{
       return grade(s.midterm, s.final, s.homework);
}

#endif // GRADE_CPP_INCLUDED

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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;
}

1
2
3
4
5
6
7
8
9
10
#ifndef MEDIAH_H_INCLUDED
#define MEDIAH_H_INCLUDED

// median.h
#include <vector>
double median(std::vector<double>);
#endif

 // MEDIAH_H_INCLUDED

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STUDENT_INFO_H_INCLUDED
#define STUDENT_INFO_H_INCLUDED


#include <iostream>
#include <string>
#include <vector>
struct Student_info {
       std::string name;
       double midterm, final;
       std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif

 // STUDENT_INFO_H_INCLUDED

--Justin
The issue is you're trying to call the function
 
double median(std::vector<double>);


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];
	}
}


I didn't test it, but that should work.
Topic archived. No new replies allowed.