title: How to create a vector of class objects in C++?
body: <p>I am trying to create a simple stack using vector in C++.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class Site
  {
      public:
          int i; // site position i (x-axis)
          int s; // site state
          vector&lt;Site&gt; neighbors;
          Site(void);
          Site(int ii, int ss);
          void AddNeighbor(Site &amp;site);
  };
  Site::Site()
  {
      i = -1;
      s = -1;
      vector&lt;Site&gt; neighbors;
  }
  Site::Site(int ii, int ss) 
  {
      i = ii;
      s = ss;
  }
  void Site::AddNeighbor(Site &amp;site)
  {
      neighbors.push_back(site);
  }
  
  void testStack()
  {
      int tot = 600;
      vector&lt;Site&gt; myStack();
      int i = 0;
      while (i &lt; tot)
      {
          Site site(i, 1);
          myStack.push_back(site);
          i++;
      }
  
      i = 0;
      while (i &lt; tot)
      {
          Site *site = myStack.back();
          myStack.pop_back();
          cout &lt;&lt; site-&gt;i &lt;&lt; site-&gt;s &lt;&lt; endl;
          i++;
      }
  }
  </code></pre>
  
  <p>Compiler errors:</p>
  
  <blockquote>
    <p>ising_wolff.cpp: In function ‘void testStack()’:
    ising_wolff.cpp:373:17: error: request for member ‘push_back’ in
    ‘myStack’, which is of non-class type ‘std::vector()’
             myStack.push_back(site);
                     ^ ising_wolff.cpp:380:30: error: request for member ‘back’ in ‘myStack’, which is of non-class type ‘std::vector()’
             Site *site = myStack.back();
                                  ^ ising_wolff.cpp:381:17: error: request for member ‘pop_back’ in ‘myStack’, which is of non-class type
    ‘std::vector()’
             myStack.pop_back();</p>
  </blockquote>
  
  <p>What do these errors mean?</p>
  
  <p>Here are some sites I have looked at:</p>
  
  <p>1) <a href="https://stackoverflow.com/questions/15802006/create-objects-while-adding-them-into-vector">Creating objects while adding them into vectors</a></p>
  
  <p>2) <a href="https://stackoverflow.com/questions/2166321/push-back-causeing-an-error-when-using-vectors-in-c">push_back causing errors in C</a></p>
  
  <p>3) <a href="http://www.cplusplus.com/forum/general/7950/" rel="nofollow noreferrer">how to create vectors of class object</a></p>