By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,516 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,516 IT Pros & Developers. It's quick & easy.

Compiling Error

P: 2
i am not able t compile one small program which has been divided into hearder files.when i compile the main program, it gives the linker error saying undefined reference to read, compare and grade functions.I compiled using dev c++ compiler.Please help me out cuz i m a new learner and i cant move ahead without completing this assignment.:(

********Grading programm.cpp*****
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"
using std::cin; using std::setprecision;
using std::cout; using std::sort;
using std::domain_error; using std::streamsize;
using std::endl; using std::string;
using std::max; using std::vector;
int main()
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0; // the length of the longest name
// read and store all the students data.
// Invariant: students contains all the student records read so far
// maxlen contains the length of the longest name in students
while (read(cin, record)) {
// find length of longest name
maxlen = max(maxlen,;
// alphabetize the student records
sort(students.begin(), students.end(), compare);
// write the names and grades
for (vector<Student_info>::size_type i = 0;
i != students.size(); ++i) {
// write the name, padded on the right to maxlen + 1 characters
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(), ' ');
// compute and write the grade
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;
return 0;

#include <algorithm>
#include <stdexcept>
#include <vector>
using std:: domain_error; using::sort;
using std::vector;
double median(vecotr<double> vec)
typedef vector<double::size_type vec_sz;
vec_sz size = vec.size();
throw domain_error("median of an empty vector");
vec_sz mid = size/2;
return size%2 == 0?(vec[mid] + vec[mid-1])/2 : vec[mid];
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.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 (hm.size()==0)
throw domain_error ("Stundent has not done homework");
return grade(midterm, final, median(hw));
double grade(student_info& s)
return grade(s.midterm,, s.homework);

#ifndef GUARD_Student_info
#define GUARD_Student_info
// Student_info.h header file
#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>&);

#ifndef GUARD_median_h
#define GUARD median_h

#include <vector>
double median(std::vector<double>);

// source file for Student_info-related functions
#include "Student_info.h"
bool compare(const Student_info& x, const Student_info& y)
return <;
std::istream& read(std::istream& is, Student_info& s)
// read and store the student's name and midterm and final exam grades
is >> >> s.midterm >>;
read_hw(is, s.homework); // read and store all the student's homework grades
return is;
// read homework grades from an input stream into a `vector'
std::istream& read_hw(std::istream& in, std::vector<double>& hw)
if (in) {
// get rid of previous contents
// read homework grades
double x;
while (in >> x)
// clear the stream so that input will work for the next student
return in;
Apr 24 '06 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
You are almost certainly failing to link the objects from grade.cpp and student info.cpp into the application.

It's possible that you are compiling them either
Apr 24 '06 #2

P: 2
Problem was solved after creating a new project folder and taking all the files into it unlike i was doing previously by compiling individual files.
Apr 25 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.