title: C++ Static Struct Type Member Initializations
body: <p>Possible duplicate: <a href="https://stackoverflow.com/questions/1563897/c-static-constant-string-class-member">link</a></p>
  
  <p>Hello everyone,</p>
  
  <p>There's a weird thing that I don't understand about static member variables. If the "definition" (I'm not sure if it is the right word for it) for static variables are in the header file of a class, compiler gives linking errors however if they are in the cpp file, everything is OK. </p>
  
  <p>I have a class like follows (didn't paste the entire thing):</p>
  
  <p><em>UserInterface.h</em></p>
  
  <pre><code>class UserInterface
  {
  public:
      UserInterface(void);
      ~UserInterface(void);
  
      // Some method declarations here
  private:
      // Some more methods declarations here
      // VARIABLES
      static bool                               m_undoRequested;
      static ChessViewConstants::MENU_STATE     m_displayState;
      static ChessModelConstants::PieceMovement m_pieceMovement;
  };
  // THESE DO NOT WORK (linking errors)
  //bool UserInterface::m_undoRequested = false;
  //ChessViewConstants::MENU_STATE UserInterface::m_displayState = ChessViewConstants::MAIN_MENU;
  //ChessModelConstants::PieceMovement UserInterface::m_pieceMovement(1, 1, 1, 1);
  </code></pre>
  
  <p><em>UserInterface.cpp</em></p>
  
  <pre><code>#include "UserInterface.h"
  
  // These do work.
  bool UserInterface::m_undoRequested = false;
  ChessViewConstants::MENU_STATE UserInterface::m_displayState = ChessViewConstants::MAIN_MENU;
  ChessModelConstants::PieceMovement UserInterface::m_pieceMovement(1, 1, 1, 1);
  
  // Implementation....
  </code></pre>
  
  <p><em>ChessConstants.h</em></p>
  
  <pre><code>namespace ChessModelConstats{
      // Some stuff here...
  
      struct PieceMovement {
  
      // A simple Constructor
      PieceMovement(int originRow = -1, int originCol = -1, 
                    int targetRow = -1, int targetCol = -1)
      : m_originRow(originRow), m_originCol(originCol),
        m_targetRow(targetRow), m_targetCol(targetCol) 
      {
      }
  
          // Members
          int m_originRow;
          int m_originCol;
          int m_targetRow;
          int m_targetCol;
      };
  
  // More stuff here....
  }
  </code></pre>
  
  <p>So why must the static variables be implemented inside the cpp file? Why can't I append at the end of the header file?</p>
  
  <p>A second question: how can I initialize the struct variable (m_pieceMovement) like the follows:</p>
  
  <pre><code>m_pieceMovement.m_originCol = -1;
  m_pieceMovement.m_originRow = -1;
  m_pieceMovement.m_targetCol = -1;
  m_pieceMovement.m_targetRow = -1;
  </code></pre>
  
  <p>It seems I'm missing a fundamental information here, don't be shy to throw in some novice tips here and there :)</p>
  
  <p>Thanks in advance,</p>
  
  <p>John John</p>
  
  <p>EDIT: Here are the linking errors:</p>
  
  <blockquote>
    <p>1>MasterController.obj : error LNK2005: "private: static bool UserInterface::m_undoRequested" (?m_undoRequested@UserInterface@@0_NA) already defined in Execution.obj
    1>MasterController.obj : error LNK2005: "private: static enum ChessViewConstants::MENU_STATE UserInterface::m_displayState" (?m_displayState@UserInterface@@0W4MENU_STATE@ChessViewConstants@@A) already defined in Execution.obj
    1>MasterController.obj : error LNK2005: "private: static struct ChessModelConstants::PieceMovement UserInterface::m_pieceMovement" (?m_pieceMovement@UserInterface@@0UPieceMovement@ChessModelConstants@@A) already defined in Execution.obj
    1>UserInterface.obj : error LNK2005: "private: static bool UserInterface::m_undoRequested" (?m_undoRequested@UserInterface@@0_NA) already defined in Execution.obj
    1>UserInterface.obj : error LNK2005: "private: static enum ChessViewConstants::MENU_STATE UserInterface::m_displayState" ?m_displayState@UserInterface@@0W4MENU_STATE@ChessViewConstants@@A) already defined in Execution.obj
    1>UserInterface.obj : error LNK2005: "private: static struct ChessModelConstants::PieceMovement UserInterface::m_pieceMovement" (?m_pieceMovement@UserInterface@@0UPieceMovement@ChessModelConstants@@A) already defined in Execution.obj
    1>D:\C++\CheatersChess\Debug\CheatersChess.exe : fatal error LNK1169: one or more multiply defined symbols found</p>
  </blockquote>