我刚编译它并且它不起作用。我不知道如何解决这些编译错误。构建消息显示
未定义引用'compare(const Student_info&,const Student_info&);'
未定义引用'read(std :: istream&,Student_info&);'
未定义引用'grade(const Student_info&);'
这有什么问题?以下是代码:
#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;
}
任何建议都会很好。谢谢!!!
答案 0 :(得分:4)
错误不是来自编译器,而是来自链接器。你的代码编译得很好,但是当链接器看到对例如需要bool compare(const Student_info&,const Student_info&);
代码才能将其与compare
函数的定义相匹配。而且没有。添加缺少的功能。