title: Why do static variables need to be declared twice in C++
body: <p>I have a header called <strong>filepaths.h</strong> which defines a number of static variables:</p>
  
  <pre><code>#ifndef FILEPATHS_H
  #define FILEPATHS_H
  
  class FilePaths {
  
  public:
  
      static QString dataFolder();
      static QString profileFolder();
  
  private:
  
      static QString dataFolder_;
      static QString profileFolder_;
  
  };
  
  }
  #endif // FILEPATHS_H
  </code></pre>
  
  <p>And I have an associated <strong>filepaths.cpp</strong> which initially looked like this:</p>
  
  <pre><code>#include "FilePaths.h"
  
  QString FilePaths::dataFolder() {
      return dataFolder_;
  }
  
  QString FilePaths::profileFolder() {
      return profileFolder_;
  }
  </code></pre>
  
  <p>However that didn't work - I got an "unresolved symbol error" linker error on all the static variables. So I've added these variables to the C++ file in this way:</p>
  
  <pre><code>#include "FilePaths.h"
  
  QString FilePaths::dataFolder_ = "";
  QString FilePaths::profileFolder_ = "";
  
  QString FilePaths::dataFolder() {
      return dataFolder_;
  }
  
  QString FilePaths::profileFolder() {
      return profileFolder_;
  }
  </code></pre>
  
  <p>And this works, however I don't understand why.</p>
  
  <p>Why do these static variables need to be defined twice? Or maybe I'm not defining them but initializing them? But still why does it need to be done? Or should I write my class differently?</p>