title: All elements in a c++ vector point to the same element
body: <p>I'm a complete beginner in c++ and everything has been going on well until now. I'm new to the idea of pointers (I'm from python), and I have this weird error. </p>
  
  <p>So basically, I created this "SearchNode" class, and found below is one of it's methods "getChildren" which should return a vector of other SearchNode instances, representing the possible cells to which a Knight (chessboard) could travel from it's current state. (BFS)</p>
  
  <p><strike>That said, when I finish pushing into my vector, all the elements suddenly point to 1st element only. Could someone help me out here?</p>
  
  <p>PS: it's a similar problem to <a href="https://stackoverflow.com/questions/3027857/c-push-back-doesnt-work-as-it-is-supposed">c++ push_back doesn&#39;t work as it is supposed</a> ... but unlike Angela (who's was writing her own compiler), I'm a total beginner in c++. Your help with be greatly appreciated.</strike></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I got rid of the int*, and used array for my state. I could now successfully search the graph (therefore the states are ok) and find the shortest path, but I couldn't seem to reconstruct the path.</p>
  
  <p>To test, I started at {0,0} and could find {4,4}, but the path, according to the getPath method was {4,4}, {3,6}, {3,6}, {3,6} ... (infinite loop of {3,6}). Is there something wrong with my parent pointers, or my getPath function? Thanks for your support in advance.</p>
  
  <pre><code>//Search class
  class SearchNode
  {
  public:
  //Variables
  SearchNode *m_parent;
  array&lt;int,2&gt; m_state; //I don't understand typedef's yet, will use them when I'm clearer with them :)
  
  //Normal Constructor
  SearchNode(array&lt;int,2&gt;&amp; state_, SearchNode *parent_=nullptr) :
  m_state(state_),
  m_parent(parent_)
  {}
  
  
  //Method to get Next reachable states. Returns instances of SearchNode.
  vector&lt;SearchNode&gt; getChildren()
  {
      int legalMoves[8][2] = {{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
  
      vector&lt;SearchNode&gt; children;
      children.reserve(8);
      for(int i=0; i&lt;8; i++)
      {
          int x = (m_state[0] + legalMoves[i][0]);
          int y = (m_state[1] + legalMoves[i][1]);
          if( (x&gt;-1) and (x&lt;9) and (y&lt;9) and (y&gt;-1)) // Within the bounds of the board
          {
              array&lt;int,2&gt; childState = {x,y};
              SearchNode childNode = SearchNode(childState,this);
              children.push_back(childNode);
          }
      }
      return children;
  }
  
  void getPath()
  {
      cout&lt;&lt;"\nPath: ";
      cout&lt;&lt;  this-&gt;print();
      SearchNode current = *this;
      unsigned int counter = 1;
      while((current.m_parent!=nullptr) and counter&lt; 10)
      {
          counter++;
          cout&lt;&lt; (current.m_parent)-&gt;print();
          current = *(current.m_parent);
      }
      cout &lt;&lt; (current.m_parent)-&gt;print();
  }
  
  string print()
  {
      stringstream out;
      out &lt;&lt; "{" &lt;&lt; this-&gt;m_state[0] &lt;&lt; "," &lt;&lt; this-&gt;m_state[1] &lt;&lt; "} ";
      return out.str();
  }
  };
  </code></pre>