title: A compilation error with C++: "Undefined symbols for architecture x86_64"
body: <p>I tried to compile &quot;grade.cpp&quot; separately, but the terminal returns &quot;Undefined symbols for architecture x86_64&quot;; How to solve this question such that I can compile &quot;grade.cpp&quot; separately?</p>
  <p>The terminal looks like this (my name is starred):</p>
  <pre><code>(base) ***************-MacBook-Pro ch4 % g++ grade.cpp
  Undefined symbols for architecture x86_64:
    &quot;median(std::__1::vector&lt;double, std::__1::allocator&lt;double&gt; &gt;)&quot;, referenced from:
        grade(double, double, std::__1::vector&lt;double, std::__1::allocator&lt;double&gt; &gt; const&amp;) 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)
  </code></pre>
  <p>The &quot;grade.cpp&quot; is:</p>
  <pre><code>#include &quot;grade.h&quot;
  #include &quot;median.h&quot;
  #include &quot;Student_info.h&quot;
  #include &lt;stdexcept&gt;
  #include &lt;vector&gt;
  
  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&lt;double&gt;&amp; hw) {
      if (hw.size() == 0){
          throw domain_error(&quot;student has done no homework&quot;);
      }
      return grade(midterm, final, median(hw));
  }
  
  //4.2.2
  double grade(const Student_info&amp; s) {
      return grade(s.midterm, s.final, s.homework);
  }
  </code></pre>
  <p>The &quot;grade.h&quot; is:</p>
  <pre><code>#ifndef GUARD_grade_h
  #define GUARD_grade_h
  
  //grade.h
  #include &lt;vector&gt;
  #include &quot;Student_info.h&quot;
  
  double grade(double, double, double);
  double grade(double, double, const std::vector&lt;double&gt;&amp;);
  double grade(const Student_info&amp;);
  
  #endif
  </code></pre>
  <p>The &quot;median.cpp&quot; is:</p>
  <pre><code>#include &quot;median.h&quot;
  #include &lt;vector&gt;
  #include &lt;stdexcept&gt;
  #include &lt;algorithm&gt;
  
  using std::domain_error;
  using std::sort;
  using std::vector;
  
  
  double median(vector&lt;double&gt; vec) {
      typedef vector&lt;double&gt;::size_type vec_sz;
      vec_sz size = vec.size();
      if (size == 0) {
          throw domain_error(&quot;median of an empty vector&quot;);
      }
      sort(vec.begin(), vec.end());
      vec_sz mid = size/2;
      return (size % 2 == 0) ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
  }
  </code></pre>
  <p>The &quot;median.h&quot; is:</p>
  <pre><code>#ifndef GUARD_median_h
  #define GUARD_median_h
  //4.3
  #include &lt;vector&gt;
  
  double median(std::vector&lt;double&gt;);
  
  #endif
  </code></pre>
  <p>The &quot;Student_info.cpp&quot; is</p>
  <pre><code>#include &quot;Student_info.h&quot;
  
  using std::istream;
  using std::vector;
  
  
  bool compare(const Student_info&amp; x, const Student_info&amp; y) {
      return x.name &lt; y.name;
  }
  
  //4.2.2
  istream&amp; read(istream&amp; is, Student_info&amp; s) {
      is &gt;&gt; s.name &gt;&gt; s.midterm &gt;&gt; s.final;
      read_hw(is, s.homework);
      return is;
  }
  
  //4.1.3
  istream&amp; read_hw(istream&amp; in, vector&lt;double&gt;&amp; hw) {
      if (in) {
          hw.clear();
  
          double x;
          while(in &gt;&gt; x) {
              hw.push_back(x);
          }
          in.clear();
      }
  
      return in;
  }
  </code></pre>
  <p>The &quot;Student_info.h&quot; is:</p>
  <pre><code>#ifndef GUARD_Student_info
  #define GUARD_Student_info
  
  //4.4
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  struct Student_info {
      std::string name;
      double midterm, final;
      std::vector&lt;double&gt; homework;
  };
  
  bool compare(const Student_info&amp;, const Student_info&amp;);
  std::istream&amp; read(std::istream&amp;, Student_info&amp;);
  std::istream&amp; read_hw(std::istream&amp;, std::vector&lt;double&gt;&amp;);
  #endif
  </code></pre>