gpt4 book ai didi

c++ - 为什么编译结果是 "undefined reference to ' function'”?

转载 作者:行者123 更新时间:2023-11-27 23:06:56 26 4
gpt4 key购买 nike

我刚刚编译了它但它没有工作。我不知道如何解决这些编译错误。构建消息显示

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

这有什么问题?这是代码:

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

任何建议都会很好。谢谢!!!

最佳答案

错误不是来自编译器,而是来自链接器。您的代码编译得很好,但是当链接器看到对例如的调用时bool compare(const Student_info&,const Student_info&); 在代码中需要将其与 compare 函数的定义相匹配。而且没有任何。添加缺少的功能。

关于c++ - 为什么编译结果是 "undefined reference to ' function'”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com