Accelerated C++: Undefined symbols for architecture x86_64

246 Views Asked by At

I'm working through Accelerated C++, and I cannot get the program from Chapter 4 to compile; it's the first one with headers. I've also downloaded the source from here, and get the exact same error I see with my hand-copied code, so I'm not sure what the issue is.

I'm using g++ on OSX El Capitan. Here's the exact command I used:

$ g++ main.cpp

Here's the error:

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)

And here's the code:

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
}

grade.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

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

median.h

#ifndef GUARD_median_h
#define GUARD_median_h

#include <vector>

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

#endif

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

Thanks so much for your help, anyone!

1

There are 1 best solutions below

1
On BEST ANSWER

As @gudok's somewhat cryptic comment suggests, this is most likely your first program with multiple source files. You can't just compile one file and get an executable; you have to compile each file and link the resulting object files together. The simplest way to do that is like this:

g++ main.cpp grade.cpp student_info.cpp median.cpp

If there are no errors in the code, that will produce an executable file named "a.out" in the current directory.

EDIT: just noticed that the error message comes from "clang", not from "g++'. That's okay; if you're using "clang", just change the command line to

clang++ main.cpp grade.cpp student_info.cpp median.cpp