A compilation error with C++: "Undefined symbols for architecture x86_64"

95 views Asked by At

I tried to compile "grade.cpp" separately, but the terminal returns "Undefined symbols for architecture x86_64"; How to solve this question such that I can compile "grade.cpp" separately?

The terminal looks like this (my name is starred):

(base) ***************-MacBook-Pro ch4 % g++ grade.cpp
Undefined symbols for architecture x86_64:
  "median(std::__1::vector<double, std::__1::allocator<double> >)", referenced from:
      grade(double, double, std::__1::vector<double, std::__1::allocator<double> > const&) in grade-924066.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The "grade.cpp" is:

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

using std::domain_error;
using std::vector;

int main() {
    return 0;
}

//4.1
double grade(double midterm, double final, double homework) {
    return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

//4.1.2
double grade(double midterm, double final, const vector<double>& hw) {
    if (hw.size() == 0){
        throw domain_error("student has done no homework");
    }
    return grade(midterm, final, median(hw));
}

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

The "grade.h" is:

#ifndef GUARD_grade_h
#define GUARD_grade_h

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

The "median.cpp" is:

#include "median.h"
#include <vector>
#include <stdexcept>
#include <algorithm>

using std::domain_error;
using std::sort;
using std::vector;


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

The "median.h" is:

#ifndef GUARD_median_h
#define GUARD_median_h
//4.3
#include <vector>

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

#endif

The "Student_info.cpp" is

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

//4.2.2
istream& read(istream& is, Student_info& s) {
    is >> s.name >> s.midterm >> s.final;
    read_hw(is, s.homework);
    return is;
}

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

The "Student_info.h" is:

#ifndef GUARD_Student_info
#define GUARD_Student_info

//4.4
#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
0

There are 0 answers