title: C++ accessing element in vector of class objects
body: <p>I'm trying to access an element within a vector of class objects but I don't get it right. I guess it's something wrong with the constructor/deconstructor and reference but even the other questions like <a href="https://stackoverflow.com/questions/2745350/c-destructor-issue-with-stdvector-of-class-objects">C++ destructor issue with std::vector of class objects</a>, <a href="https://stackoverflow.com/questions/1798631/c-vector-of-class-object-pointers">c++ vector of class object pointers</a> or <a href="https://stackoverflow.com/questions/6624819/c-vector-of-objects-vs-vector-of-pointers-to-objects">C++ vector of objects vs. vector of pointers to objects</a>. Hopefully someone can help me to fix my code snippet.</p>
  
  <h2>node.h</h2>
  
  <pre><code>class Node {
  
  public:
      Node();
      Node(int id, const std::string&amp; name);
      Node(const Node&amp; orig);
      virtual ~Node();
  
      void cout(void);
  
      int get_id(void);
  
  private:
      int _id;
      std::string _name;
  
  };
  </code></pre>
  
  <h2>node.cpp</h2>
  
  <pre><code>#include "node.h"
  
  Node::Node() {
  }
  
  Node::Node(int id, const std::string&amp; name) : _id(id), _name(name) {
      this-&gt;cout();
  }
  
  Node::Node(const Node&amp; orig) {
  }
  
  Node::~Node() {
  }
  
  void Node::cout(void) {
      std::cout &lt;&lt; "NODE " &lt;&lt; _id &lt;&lt; " \"" &lt;&lt; _name &lt;&lt; "\"" std::endl;
  }
  
  int Node::get_id(void) {
      return _id;
  }
  </code></pre>
  
  <h2>communication.h</h2>
  
  <pre><code>#include "node.h"
  
  class Com {
  
  public:
      std::vector&lt;Node&gt; nodes;
  
      Com();
      com(const Com&amp; orig);
      virtual ~Com();
  
      void cout_nodes(void);
  
  private:
  
  };
  </code></pre>
  
  <h2>communication.cpp</h2>
  
  <pre><code>#include "communication.h"
  
  Com::Com() {
      nodes.push_back(Node(169, "Node #1"));
      nodes.push_back(Node(170, "Node #2"));
  }
  
  Com::Com(const Com&amp; orig) {
  }
  
  Com::~Com() {
  }
  
  void Com::cout_nodes(void) {
      for (uint8_t i = 0; i &lt; nodes.size(); i++) {
          nodes[i].cout();
      }
  }
  </code></pre>
  
  <p>If I run <code>Com com;</code> I get the expected output of:</p>
  
  <pre><code>[I 171218 13:10:10 Cpp:22] &lt; NODE 169 "Node #1"
  [I 171218 13:10:10 Cpp:22] &lt; NODE 170 "Node #2"
  </code></pre>
  
  <p>but running <code>com.cout_nodes();</code> results in:</p>
  
  <pre><code>[I 171218 13:10:14 Cpp:22] &lt; NODE 0 ""
  [I 171218 13:10:14 Cpp:22] &lt; NODE 0 ""
  </code></pre>
  
  <p>As in <a href="https://stackoverflow.com/questions/6624819/c-vector-of-objects-vs-vector-of-pointers-to-objects">C++ vector of objects vs. vector of pointers to objects</a> everything works fine when I use references but I couldn't get <code>std::iterator</code> and <code>find_if</code> working than.</p>
  
  <h3>update: working <code>find_if</code> statement and index calculation</h3>
  
  <pre><code>auto iterator = std::find_if(nodes.begin(), nodes.end(), [&amp;](Node node) {
      return node.get_id() == 169;
  });
  
  if (iterator != nodes.end()) {
      size_t index = std::distance(nodes.begin(), iterator );
      std::cout &lt;&lt; "Index of ID #169: " &lt;&lt; index &lt;&lt; std::endl;
  }
  </code></pre>