#ifndef GUARD_student_info_h
#define GUARD_student_info_h
#include <vector>
#include <string>
#include <iostream>
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>&);
#endif
該函數的錯誤碼是student_info.h頭文件C++與頭文件發放該未定義
#include "student_info.h"
using std::istream; using std::vector;
bool compare(const student_info& x, const student_info& y)
{
return x.name < y.name;
}
istream& read(istream& is, student_info & s)
{
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework);
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if(in)
{
hw.clear();
double x;
while(in >> x)
hw.push_back(x);
in.clear();
}
return in;
}
這是student_info.cpp
#ifndef GUARD_median_h
#define GUARD_median_h
#include <vector>
double median(std::vector<double>);
#endif
這是median.h
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
using std::domain_error;
using std::vector;
double median(vector<double>vec) // No need of saying it as vector<double>homework, cos we wont be using this to always give median of hw, but median of some other data
{
typedef vector<double>::size_type vec_sz;
vec_sz size;
size = vec.size();
if(size == 0)
throw domain_error("Empty homework. From medain.cpp when vec.size() == 0.");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? vec[mid] + vec[mid - 1]/2 : vec[mid];
}
這是median.cpp
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include <vector>
#include "student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const student_info&);
#endif
這是grade.h
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "student_info.h"
using namespace std;
using std::domain_error;
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(hw.size() == 0)
throw domain_error("No homework. From grades function 2 in grade.cpp.");
grade(midterm, final, median(hw));
}
double grade(const student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
這是grade.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <ios>
#include <stdexcept>
#include "grade.h"
#include "student_info.h"
using namespace std;
using std::streamsize;
using std::setprecision;
using std::domain_error;
using std::max;
istream& read(istream& , 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) {
// 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;
}
這是main.cpp中
當我編譯它,我得到以下錯誤:
undefined reference to `read(std::istream&, student_info&)'
undefined reference to `compare(student_info const&, student_info const&)'
undefined reference to `grade(student_info const&)'
collect2: error: ld returned 1 exit status
發生了什麼事。有人能糾正它,讓我知道是什麼問題。
添加編譯器行。我想你只是忘了添加所有的源文件。此外,這是太多的代碼。 – Zeta 2013-03-06 13:55:10
您不用所有源文件構建。請編輯您的問題以顯示您如何構建您的項目。 – 2013-03-06 13:56:14
你從哪裏得到這些未定義的參考?在哪個文件? – 2013-03-06 13:56:19