Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:55:16+00:00 2026-05-31T05:55:16+00:00

I’m trying to build a list inside a class in C++ and I don’t

  • 0

I’m trying to build a list inside a class in C++ and I don’t understand what I’m doing wrong

class Car(){  
public:  
    Car();  
    ~Car();  
    list <Objects> myList;  //a list of some objects from one of my classes  
    list <Objects>::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);  
} 

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

Everything is sorted now with inserting, however I’m having problems removing from the same list.
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:

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???
}  

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?
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:55:18+00:00Added an answer on May 31, 2026 at 5:55 am

    Your list will be initialised automatically in your constructor and you don’t need an iterator as a class member. To add your object to the list you should just call push_back:

    void addObject(Object obj){  
    myList.push_back(obj);  
    }  
    

    See http://www.cplusplus.com/reference/stl/list/push_back/

    To insert at a specific location see: http://www.cplusplus.com/reference/stl/list/insert/

    If you are going to arbritrarily decide where to insert your new object then you will need to determine where each time using find(), if you were going to always insert at beginning or end then it’s easy enough to call

    // insert in arbitrary location
    myList::iterator it = myList.find(val);
    myList.insert(it, obj);
    // OR insert at beginning
    myList.push_front(obj); // to insert at end see above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.