title: c++ Compile error - Undefined symbols for architecture x86_64
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5818270/undefined-symbols-for-architecture-i386">Undefined symbols for architecture i386:</a>  </p>
  </blockquote>
  
  
  
  <pre><code>using namespace std;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include "CorpusExp.h"
  
  int main(int argc, char* argv[]){
  ifstream infile("../DATA.txt");
  string train_dir;   // The training data directory
  infile&gt;&gt;train_dir;
  train_dir=train_dir.substr(6,train_dir.length());
  
  if (argc&gt;1){
      if (strcmp(argv[1],"-s")){ // enter into CorpusExploration mode
          CorpusExp ce(train_dir,"all");   //&lt;&lt;=======LINE X!!!!!!!!
          //ce.calculate();
          if (argc&gt;=3 &amp;&amp; strcmp(argv[2],"-u")){ // check user stats
              cout&lt;&lt;"shit";
          }
          else if (argc&gt;=3 &amp;&amp; strcmp(argv[2],"-m")){ // check movie stats
  
          }else{ // check the all (default) stats
  
          }
      }else if(strcmp(argv[1],"-m")) {// enter into Recommendation mode
  
      }
  }
  
  return 0;
  }
  </code></pre>
  
  <p><strong>If I include the LINE X, the main.cpp won't compile, the error message is:</strong></p>
  
  <pre><code>      Undefined symbols for architecture x86_64:
     "CorpusExp::CorpusExp(std::basic_string&lt;char, std::char_traits&lt;char&gt;,     std::allocator&lt;char&gt; &gt; const&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)", referenced from:
        _main in main.o
  ld: symbol(s) not found for architecture x86_64
  collect2: ld returned 1 exit status
  make: *** [recommend] Error 1
  </code></pre>
  
  <p><strong>And here is my CorpusExp.cpp and CorpusExp.h:</strong></p>
  
  <p>/* CorpusExporater header file
     Used for statistical purpose</p>
  
  <p>*/</p>
  
  <pre><code>using namespace std;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;dirent.h&gt;  // the Linux library to read the directories
  
  class CorpusExp{
  public:
      CorpusExp(const string&amp;,const string&amp;); // (dir_name, mode)
      void calculate();
      void display(ostream &amp;out, const int&amp;);    // print out the id's stat information
  
  private:
      string _dir_name;
      int _movie_count;
      int _user_count;
      int _movie_rated_count[5]; // times of movie rated as 1,2,3,4,5
      float _movie_avg;   // total moview average rates
      string _mode; // the mode of current task
      int _id; // the id of current task
  
  };
  
  /*CorpusExporater cpp file
     Used for statistical purpose
  
  */
  #include "CorpusExp.h"
  
  CorpusExp::CorpusExp(const string &amp;dir_name, const string &amp;mode):
      _dir_name(dir_name),
      _mode(mode),
      _movie_count(0),
      _user_count(0)
  {
  
  
  }
  
  void CorpusExp::calculate(){
      DIR *dpdf;
      struct dirent *epdf;
  
      dpdf = opendir(strdup(_dir_name.c_str()));
      if (dpdf!=NULL){
          while(epdf = readdir(dpdf)){
              cout&lt;&lt;epdf-&gt;d_name&lt;&lt;endl;
          }
      }
  }
  void CorpusExp::display(ostream &amp;out, const int &amp;id){
  
  }
  </code></pre>
  
  <p><strong>I am barely new to c++, so I get very confused, don't know which part caused this problem. If I remove the LINE X, everything will just be fine, but if I declare the CorpusExp class in the main function, then it did not compile......</strong></p>
  
  <p>here is the compile file:</p>
  
  <pre><code># Makefile for Movie Recommendation--- HW5 of Search Engine 11-641
  #   Yitong Zhou
  #   Nov 19, 2012
  
  #source code directory:
  src=src
  #obj code directory:
  obj=obj
  
  
  recommend: $(obj)/main.o $(obj)/CorpusExp.o
      g++ -o $@ $&lt;
  $(obj)/main.o: src/main.cpp src/CorpusExp.cpp
      g++ -c $&lt; -o $@
  $(obj)/CorpusExp.o: $(src)/CorpusExp.cpp $(src)/CorpusExp.h
      g++ -c $&lt; -o $@
  
  clean:
      rm -rf obj/*.o recommend
  </code></pre>