zhean1874
zhean1874

Reputation: 19

Why the compiling result is "undefined reference to 'function'"?

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!!!

Upvotes: 0

Views: 905

Answers (1)

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

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.

Upvotes: 4

Related Questions