title: G++ undefined reference to class::function
body: <p>I'm finally pretty desperate. So, in my c++ class we were instructed to use classes. We'd have the header file declare the class and functions while a separate .cpp file implements the it. Things should be working, but they're not and no solutions on the web seem to be working for me. I'm using the G++ compiler on linux for this, and it doesn't seem to work on either IDE's or the normal command line. </p>
  
  <p>The error I'm getting in my TBook.h is this:</p>
  
  <pre><code>/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
  (.text+0x20): undefined reference to `main'
  /tmp/ccxqI6An.o: In function `TBook::TBook()':
  TBook.cpp:(.text+0x3b): undefined reference to `Telephone::Telephone()'
  TBook.cpp:(.text+0x100): undefined reference to `Telephone::Telephone()'
  TBook.cpp:(.text+0x132): undefined reference to `Telephone::allNum(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)'
  TBook.cpp:(.text+0x182): undefined reference to `Telephone::~Telephone()'
  TBook.cpp:(.text+0x191): undefined reference to `Telephone::~Telephone()'
  TBook.cpp:(.text+0x2b3): undefined reference to `Telephone::~Telephone()'
  TBook.cpp:(.text+0x2e6): undefined reference to `Telephone::~Telephone()'
  TBook.cpp:(.text+0x2fa): undefined reference to `Telephone::~Telephone()'
  /tmp/ccxqI6An.o:TBook.cpp:(.text+0x370): more undefined references to `Telephone::~Telephone()' follow
  /tmp/ccxqI6An.o: In function `TBook::write()':
  TBook.cpp:(.text+0x4e1): undefined reference to `Telephone::getNumber()'
  TBook.cpp:(.text+0x506): undefined reference to `Telephone::getAreaCode()'
  TBook.cpp:(.text+0x53a): undefined reference to `Telephone::getName()'
  /tmp/ccxqI6An.o: In function `TBook::lookup(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)':
  TBook.cpp:(.text+0x6d4): undefined reference to `Telephone::getName()'
  TBook.cpp:(.text+0x79e): undefined reference to `Telephone::Telephone(int, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)'
  /tmp/ccxqI6An.o: In function `TBook::print()':
  TBook.cpp:(.text+0x880): undefined reference to `Telephone::getName()'
  TBook.cpp:(.text+0x8e0): undefined reference to `Telephone::getNumber()'
  TBook.cpp:(.text+0x8ff): undefined reference to `Telephone::getAreaCode()'
  collect2: ld returned 1 exit status
  [Finished in 0.3s with exit code 1]
  </code></pre>
  
  <p>I'm kind of not liking that the file is not receiving any of the Telephone class's methods. Here's what the code looks like for TBook.h:</p>
  
  <pre><code>#ifndef TBOOK_H
  #define TBOOK_H
  
  #include "Telephone.h"
  
  class TBook{
  private:
      Telephone rolodex[10]; 
      int current;
      int max;
  public:
      TBook();
      ~TBook();
      void add(Telephone);
      void write();
      bool is_full();
      void print();
      void check();
      Telephone lookup(string);
  
  };
  
  #endif
  </code></pre>
  
  <p>And this is what TBook.cpp looks like:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  #include "TBook.h"
  #include "Telephone.h"
  
  
  using namespace std;
  
  TBook::TBook(){
      current = 0;
      max = 9;
  
      cout &lt;&lt; "Hello" &lt;&lt; endl;
  
      string line;
      ifstream myfile ("rolodex.txt");
      if (myfile.is_open()){
          while ( getline (myfile,line) ){
              cout &lt;&lt; line &lt;&lt; endl;
              Telephone t;
              t.allNum(line);
              add(t);
  
          }
          myfile.close();
      }else if (!myfile.is_open()){
          ofstream myfile;
          myfile.open ("rolodex.txt");
          myfile &lt;&lt; "This is an empty file (Relatively).";
          myfile.close();
      }
  }
  
  TBook::~TBook(){
  
  }
  
  void TBook::add(Telephone tel){
      if (!is_full()){
          rolodex[current] = tel;
          current++;
      }
  }
  
  void TBook::write(){
      ofstream myfile;
      myfile.open ("rolodex.txt");
      for (int i = 0; i &lt; current; ++i)
      {
          myfile &lt;&lt; rolodex[i].getName() &lt;&lt; "," &lt;&lt; rolodex[i].getAreaCode() &lt;&lt; "," &lt;&lt; rolodex[i].getNumber() &lt;&lt; "\n";
      }
      myfile.close();
  }
  
  bool TBook::is_full(){
      if (current &lt;= max){
          return false;
      }
      return true;
  }
  
  Telephone TBook::lookup(string lookName){
      for (int i = 0; i &lt; current; ++i){
          if (rolodex[i].getName() == lookName){
              return rolodex[i];
          }
      }
      return Telephone(100, "", "1000000");
  }
  
  void TBook::print(){
      //Print the vairables
      for (int i = 0; i &lt; current; ++i){
          cout &lt;&lt; "Name: " &lt;&lt; rolodex[i].getName() &lt;&lt; endl;
      cout &lt;&lt; "Number: (" &lt;&lt;  rolodex[i].getAreaCode() &lt;&lt; ") " &lt;&lt; rolodex[i].getNumber() &lt;&lt; endl;
      }
  
  }
  
  void TBook::check(){
      cout &lt;&lt; "the message" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Since the problem seems to be arising with the Telephone class, I figure I should also show that code too</p>
  
  <blockquote>
    <p>Telephone.h</p>
  </blockquote>
  
  <p>..</p>
  
  <pre><code>#ifndef TELEPHONE_H
  #define TELEPHONE_H
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  using std::string;
  
  class Telephone{
  private:
      string name;
      string num;
      int areaCode;
  public:
      Telephone(int, string, string);
      Telephone();
      ~Telephone();
      bool setAreaCode(int);
  
      //Setters
  
      void setName(string);
      void setNumber(string);
      bool allNum(string);
  
      //Getters
      string getName();
      string getNumber();
      int getAreaCode();
  
      //Checks
      bool checkX(int);
      bool checkY(int);
  
  
  };
  
  #endif
  </code></pre>
  
  <blockquote>
    <p>Telephone.cpp</p>
  </blockquote>
  
  <p>..</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "Telephone.h"
  
  using namespace std;
  
  Telephone::Telephone(){
      areaCode = 0
      name = "";
      num = "";
  }
  
  Telephone::Telephone(int aCode, string nam, string number){
     areaCode = aCode;
     name = name;
  
  }
  
  Telephone::~Telephone(){
      //Nope Nada
  }
  
  bool Telephone::allNum(string all){
      size_t found =  all.find_first_of(",");
  
      //
      string first = all.substr(0, found);
      string second = all.substr((found)+1, found+1);
      string third = all.substr( all.find_last_of(",")+1, all.length());
      int x, y;
      //convert string to int values
      if(third.length() == 7){
          x = atoi(third.substr(0,3).c_str()), 
          y = atoi(third.substr(3,4).c_str());
      }else{
          cerr &lt;&lt; "Your phone number is not valid" &lt;&lt; endl;
      }   
      int ac = atoi(second.substr(0, second.length()).c_str());
  
      setName(first);
      if (!setAreaCode(ac)){  
          setAreaCode(100);
          return true;
  
      }
      if (!checkX(x) || !checkY(y)){
              setNumber("1000000");
          }else{
              setNumber(third);
      }
  
      cerr &lt;&lt; "The info provided is not valid" &lt;&lt; endl;
      return false;
  }
  
  void Telephone::setNumber(string number){
      num = number;
  }
  
  bool Telephone::setAreaCode(int aCode){
      if(aCode &gt;= 100 &amp;&amp; aCode &lt;= 999){
          areaCode = aCode;
          return true;
      }
      return false;
  }
  
  void  Telephone::setName(string theName){
      name = theName;
  }
  
  bool Telephone::checkX(int x){
      if(x &gt;= 100 &amp;&amp; x &lt;= 999){
          return true;
      }
      cerr &lt;&lt; "First three digits are not valid" &lt;&lt; endl;
      return false;
  }
  
  bool Telephone::checkY(int y){
      if(y &gt;= 0000 &amp;&amp; y &lt;= 9999){
          return true;
      }
      cerr &lt;&lt; "Last four digits are not valid" &lt;&lt; endl;
      return false;
  }
  
  
  
  //Getters
  string Telephone::getName(){
      return name;
  }
  
  string Telephone::getNumber(){
      return num;
  }
  
  int Telephone::getAreaCode(){
      return areaCode;
  }
  </code></pre>
  
  <p>And my main file (also called test.cpp) looks like this:</p>
  
  <blockquote>
    <p>test.cpp</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "TBook.h"
  #include "Telephone.h"
  
  using namespace std;
  
  
  int main(int argc, char const *argv[])
  {
      //Create a Rolodex
      TBook addressBook;
  
      return 0;
  }
  </code></pre>
  
  <p>I'm also getting this error with test.cpp</p>
  
  <pre><code>/tmp/ccl8anRb.o: In function `main':
  test.cpp:(.text+0x24): undefined reference to `TBook::TBook()'
  test.cpp:(.text+0x38): undefined reference to `TBook::~TBook()'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I think this is mostly a compiling error, but I'm still not sure, and I feel like I'm the setup of the meme "My code doesn't work, and I don't know why." Usually I would bash the code and try different methods until it works, but I simply don't have the time. I therefore need your help. </p>