0

I had just compiled it and it did not work.And I don't know how to solve these compiling errors.the Build Messages shows that

undefined reference to 'compare(const Student_info&,const Student_info&);'
undefined reference to 'read(std::istream&, Student_info&);'
undefined reference to 'grade(const Student_info&);'

what's wrong with that?Here is the code:

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using std::cin;
using std::cout;
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;

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>&);

double grade(double,double,double);
double grade(double,double,const std::vector<double>&);
double grade(const Student_info&);

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);
}
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;
}
return 0;
}

Any suggestion would be good.thanks!!!

2
  • 5
    Like it says, where are your definitions of compare and read? Commented Apr 10, 2014 at 12:47
  • 1
    You provided the "notice" that you will be providing those functions, but didn't include the function body. Commented Apr 10, 2014 at 12:48

1 Answer 1

4

The errors are not from the compiler but from linker. Your code compiles fine, but then when linker sees the call to e.g. bool compare(const Student_info&,const Student_info&); in the code it needs to match it with the definition of the compare function. And there isn't any. Add missing functions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.