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 7807303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:48:50+00:00 2026-06-02T02:48:50+00:00

I have many classes inherited from the abstract base SysLaserBase. I add every laser

  • 0

I have many classes inherited from the abstract base SysLaserBase. I add every laser to a list in the program (vector of pointers to the objects) as you see in the function addLaser(). This works perfectly fine, and it’s totally working, and I can cycle through all the lasers I created and use their methods with no problems.

The problem happens then when I want to “remove” a laser from the system. I do this by comparing the address of the object with all the addresses in the vector of the base class’s pointers. Is this legal? it’s causing the program to crash some times. What’s the alternative?

Please find here the code of the methods, and then an example of the call that causes the crash. Do you find anything wrong in the code of removeLaser()? why does the whole system act crazy after the removal of a single laser?

the vector involved is in the base class’s body:

std::vector<SysLaserBase<T>*> lasers;

And the add and remove methods:

template <typename T>
void SysSystemBase<T>::addLaser(const SysLaserBase<T> &src)
{
    bool alreadyThere = 0;
    for(unsigned long i = 0; i < lasers.size(); i++)
    {
        if(&src == lasers[i])
        {
            alreadyThere = 1;
            break;
        }
    }
    if(!alreadyThere)
    {
        lasers.push_back(const_cast<SysLaserBase<T>*>(&src));
    }
    signalsOut.resize(lasers.size());
}


template <typename T>
void SysSystemBase<T>::removeLaser(const SysLaserBase<T> &src)
{
    for(typename std::vector<SysLaserBase<T>*>::iterator it = lasers.begin(); it != lasers.end(); ++it)
    {
        if((*it) == &src)
        {
            lasers.erase(it);
        }
    }
    signalsOut.resize(lasers.size());
}

Call of this code:

sys.addLaser(las0);
sys.addLaser(las1);
sys.addLaser(las2);
sys.removeLaser(las0); //removes the laser, but cycling through the lasers through the vector of base class causes a crash
sys.removeLaser(las1);
sys.removeLaser(las2); //crashes the program immediately after this call

Thank you for any efforts 🙂


Thank you for the replies. According to your replies I have changed removeLaser() to this:

template <typename T>
void SysSystemBase<T>::removeLaser(const SysLaserBase<T> &src)
{
    for(typename std::vector<SysLaserBase<T>*>::iterator it = lasers.begin(); it != lasers.end(); ++it)
    {
        if((*it) == &src)
        {
            lasers.erase(it);
            break;
        }
    }
    signalsOut.resize(lasers.size());
}

It’s OK now.
Thanks 🙂

  • 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-06-02T02:48:53+00:00Added an answer on June 2, 2026 at 2:48 am

    it is invalidated after the lasers.erase(). From std::vector::erase():

    Iterators and references to the erased elements and to the elements between them and the end of the container are invalidated.

    Either break from the for or save the return value from erase().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a abstract base class that I have many inherited classes coming off
What I have? I have an abstract class QueryExecutor and many classes inheriting from
I have one abstract class and many child classes. In child classes are from
I have an abstract base class that many classes extend. I'd like all these
I have a stateless, abstract base class from which various concrete classes inherit. Some
I have a base class DockedToolWindow : Form, and many classes that derive from
I have a bunch of stored, serialized classes (that all inherit from a base
In my project I have a class that is inherited by many other classes.
So I have many child classes that will inherit from a parent class. I've
I'm working on a large project (for me) which will have many classes and

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.