gpt4 book ai didi

c++ - 加速 C++ : Undefined symbols for architecture x86_64

转载 作者:行者123 更新时间:2023-11-28 05:42:41 25 4
gpt4 key购买 nike

<分区>

我正在使用 Accelerated C++,但无法编译第 4 章中的程序;这是第一个带有标题的。我还从 here 下载了源代码, 并得到与我用手复制的代码看到的完全相同的错误,所以我不确定问题是什么。

我在 OSX El Capitan 上使用 g++。这是我使用的确切命令:

$ g++ main.cpp

这是错误:

Undefined symbols for architecture x86_64:
"read(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Student_info&)", referenced from:
_main in grades-main-e0475a.o
"grade(Student_info const&)", referenced from:
_main in grades-main-e0475a.o
"compare(Student_info const&, Student_info const&)", referenced from:
_main in grades-main-e0475a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

代码如下:

main.cpp

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using namespace std;

int main() {

vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0; // length of the longest name

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; // success
}

等级.h

#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

等级.cpp

#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"

using namespace std;

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 doman_error("student has done no homework");

return grade(midterm, final, median(hw));
}

double grade(const Student_info& s) {
return grade(s.midterm, s.final, s.homework);
}

Student_info.h

#ifndef GUARD_Student_info
#define GUARD_Student_info

#include <iostream>
#include <string>
#include <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>&);

#endif

Student_info.cpp

#include "Student_info.h"

using namespace std;

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

中位数.h

#ifndef GUARD_median_h
#define GUARD_median_h

#include <vector>

double median(std::vector<double>);

#endif

中位数.cpp

#include <algorithm>
#include <stdexcept>
#include <vector>

using namespace std;

double median(vector<double> vec) {
typedef vector<double>::size_type vec_sz;

vec_sz size = vec.size();
if (size == 0) throw domain_error("median of an empty vector");

sort(vec.begin(), vec.end());

vec_sz mid = size / 2;

return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

非常感谢您的帮助,任何人!

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