title: C++ Compiling error: Undefined symbols for architecture x86_64
body: <p>I'm having difficulty getting this to compile. I think the it has to do with the static variables, but I'm not 100% sure what I'm doing. Here is the error message I keep getting:</p>
  
  <blockquote>
    <p>Undefined symbols for architecture x86_64:
      "Counter::nCounters", referenced from:
          Counter::Counter(int, int) in main.o</p>
    
    <p>Counter::getNCounters() in main.o</p>
    
    <p>Counter::Counter(int, int) in Counter.o</p>
    
    <p>Counter::getNCounters() in Counter.o</p>
    
    <p>ld: symbol(s) not found for architecture x86_64</p>
    
    <p>clang: error: linker command failed with exit code 1 (use -v to see invocation)</p>
  </blockquote>
  
  <p>Here is the header file:</p>
  
  <pre><code>#ifndef project1_Counter_h
  #define project1_Counter_h
  
  class Counter
  {
  private:
  int counter;
  int limit;
  static int nCounters;
  
  public:
  Counter(int, int);
  void increment();
  void decrement();
  int getValue();
  static int getNCounters();
  };
  
  #endif
  </code></pre>
  
  <p>And here is the .cpp file:</p>
  
  <pre><code>#include "Counter.h"
  
  Counter::Counter(int a, int b)
  {
  counter = a;
  limit = b;
  nCounters++;
  }
  
  void Counter::increment()
  {
  if (counter &lt; limit)
      counter++;
  }
  
  void Counter::decrement()
  {
  if (counter &gt; 0)
      counter--;
  }
  
  int Counter::getValue()
  {
  return counter;
  }
  
  int Counter::getNCounters()
  {    
  return nCounters;
  }
  </code></pre>
  
  <p>And the main.cpp is just a simple Hello World program. Any help would be appreciated.</p>