title: Instantiate a list inside a constructor in C++
body: <p>I'm trying to build a list inside a class in C++ and I don't understand what I'm doing wrong  </p>
  
  <pre><code>class Car(){  
  public:  
      Car();  
      ~Car();  
      list &lt;Objects&gt; myList;  //a list of some objects from one of my classes  
      list &lt;Objects&gt;::iterator it;  
      void addObject(Object obj);  
      void removeObject(Object obj);  
  };  
  
  Car::Car(){  
      //what do I need to add to initialize the list
      it = myList.begin();
  };  
  
  void addObject(Object obj){  
      //myList.insert(it, obj);  
      myList.push_back(obj);
  }  
  
  void removeObject(Object obj){  
      myList.remove(obj);  
  } 
  </code></pre>
  
  <p>I don't get any build errors, however the debugger shouts at me "Expression: list insert iterator outside range". I believe I'm not initializing the list inside my constructor and that's the reason why. How can I do it?
  Thanks</p>
  
  <p>Everything is sorted now with inserting, however I'm having problems removing from the same list.<br>
  I presume it has to do with these "lovely" pointers. To get this once and for good, in my main I'm creating an object of type Car. Then I create some objects of type Object. Then I add them to my Car object using the above code. Why can't I remove them? And most important: how many instances of the same object did I create using this code:  </p>
  
  <pre><code>int main(){  
      Car myCar = Car();  
      Object one = Object("first object");  
      Object two = Object("second object");  
      Car.addObject(one);  
      Car.addObject(two);  
      Car.removeObject(one);  //doesn't work???
  }  
  </code></pre>
  
  <p>Could someone please explain to me what am I doing wrong? Does anything have to do with my passing objects by value? What is the correct way in doing the above?<br>
  Thank you.</p>