title: Undefined Reference To Member function
body: <p>I'm trying to solve a compiling problem regarding outside defined constructor.</p>
  
  <p>Here's 2 of the classes that have this problem:</p>
  
  <pre><code>//Username.h
  
  #ifndef USERNAME_H
  #define USERNAME_H
  
  #include &lt;string&gt;
  using namespace std;
  
  class Username{
  private:
      string Name;
  public:
      Username(string = "");
      string getName() const;
      void setName(string);
  };
  
  #endif
  </code></pre>
  
  <p>...</p>
  
  <pre><code>//Username.cpp
  
  #include "Username.h"
  
  Username::Username(string n):Name(n){}
  string Username::getName() const{return Name;}
  void Username::setName(string n){Name = n;}
  </code></pre>
  
  <p>...</p>
  
  <pre><code>//User.h
  
  #ifndef USER_H
  #define USER_H
  
  #include &lt;string&gt;
  #include "Username.h"
  using namespace std;
  
  class User{
  protected:
          Username* UserUsername;
  public:
      User(string s);
      virtual ~User();
      Username* getUsername() const;
      void setUsername(Username*);
  };
  
  #endif
  </code></pre>
  
  <p>...</p>
  
  <pre><code>//User.cpp
  
  #include "User.h"
  
  User::User(string s):UserUsername(new Username(s)){}
  
  User::~User(){}
  
  Username* User::getUsername() const{return UserUsername;}
  
  void User::setUsername(Username* u){UserUsername=u;}
  
  int main(){return 0;}
  </code></pre>
  
  <p>If I compile using "g++ User.cpp" I get this error:</p>
  
  <pre><code>/tmp/ccEmWmfl.o: In function `User::User(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)':
  
  User.cpp:(.text+0x3e): undefined reference to `Username::Username(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;)'
  
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>If I use "g++ User.cpp Username.cpp -o main" or if I use inline constructors/destructors I get no error.</p>
  
  <p>These classes are very easy, but I have tons more to compile that require more than one class.</p>
  
  <p>I'm new at compiling in Ubuntu shell with g++, so please, can someone help me to understand?</p>