title: how to link header files in c++
body: <p>I'm new to programming in C++ with header files. This is my current code:</p>
  
  <pre><code>//a.h
  #ifndef a_H
  #define a_H
  namespace hello
  {
    class A
    {
      int a;
      public:
        void setA(int x);
        int getA();
    };
  } 
  #endif
  
  //a.cpp
  #include "a.h"
  namespace hello
  {
     A::setA(int x)
    {
      a=x;
    }
    int A::getA()
    {
      return a;
    }
  }
  
  //ex2.cpp
  #include "a.h"
  #include&lt;iostream&gt;
  using namespace std;
  
  namespace hello
  {
    A* a1;
  }
  using namespace hello;
  int main()
  {
    a1=new A();
    a1-&gt;setA(10);
    cout&lt;&lt;a1-&gt;getA();
    return 1;  
  }
  </code></pre>
  
  <p>When I try to compile it with <code>g++ ex2.cpp</code>, I get this error:</p>
  
  <pre><code>In function `main':
  ex2.cpp:(.text+0x33): undefined reference to `hello::A::setA(int)'
  ex2.cpp:(.text+0x40): undefined reference to `hello::A::getA()'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Why isn't it working, and how can I fix it?</p>