How to call a function on an individual element of a 2d vector? [closed]

I’m trying to make a Breakout game in C++ and I have a problem I’ve been stuck on for 6 days (ahhh!)

I get an “invalid parameter passed to a function that considers invalid parameters fatal” exception with this code:

Edit: here’s my mre hope it helps

Edit: I updated the MRE to make more sense but Ninja Retired already solved it and it works so i guess this is a done problem (the main code (as in the bigger solution that I made this mre from) still doesn’t work though.)

Brick.h:

#pragma once
#include <vector>

class Brick
{
private:
    std::vector<std::vector<Brick>> bricks;
    bool isAlive;
public:
    Brick()
    {
        isAlive = true;
    }

    bool isBrickAlive() const
    {
        return isAlive;
    }

    void deleteBricks()
    {
        isAlive = false;
    }

    std::vector<std::vector<Brick>>& getBricks()
    {
        return bricks;
    }
};

Engine.h

#include "brick.h"
#include <vector>
#include <SFML/Graphics.hpp>

class Engine
{
private:
    sf::VideoMode vm;
    sf::RenderWindow window;
    bool running = true;
    Brick bricks;
public:
    Engine() 
    { 
        vm.width = 600, vm.height = 400;
        window.create(vm, "thing");
    }

    bool getRunning()
    {
        return running;
    }

    void input()
    {
        //input stuff
    }

    void update()
    {
        std::vector<std::vector<Brick>>::iterator it1;
        std::vector<Brick>::iterator it2;

        for (it1 = bricks.getBricks().begin(); it1 != bricks.getBricks().end(); it1++) {
            for (it2 = it1->begin(); it2 != it1->end(); it2++) {
                if (it2->isBrickAlive()) {
                    it2->deleteBricks();
                }
            }
        }
    }
    void draw()
    {
        //drawing stuff
    }
};

Main.cpp:

#include "engine.h"

int main()
{
    Engine engine;
    while (engine.getRunning())
    {
        engine.input();
        engine.update();
        engine.draw();
    }
}

I included some stuff like draw and input so that it makes sense like as a game. Kinda like for context.

Please tell me if I need to add (or delete) anything and please be nice, I know it’s none of your business but as you can imagine this is stressing me out.

I’ve tried a lot of things before this like using a for-range loop, for-each() , a regular for loop where I ran into the same problem. A lot of other things before that that I can’t remember (in hindsight I maybe should’ve written them down as I was doing them).

  • 3

    please post a minimal reproducible example and the compiler error message. Do you really have that loops right in the definition of the class?

    – 

  • I edited the description. I’m sorry I forgot to post the main loop. Also, I’ve tried making a minimal reproducible example twice, but it’s really big. should I post it?

    – 




  • 2

    the code you posted will cause a totally different error godbolt.org/z/9feTf47nM. Please read about minimal reproducible example. Adding a main is a step in the right direction, but its by far not all what makes a complete example, and the complete error message is missing too

    – 




  • 3

    getBricks returns a copy of the vector so the iterators you’re using point to completely different containers. Return a reference instead.

    – 




  • If you want help with code it is in your best interest to provide code that is correct except for the one thing you are asking about (and maybe a few things you don’t know about yet). We can’t do much to help you without a good example.

    – 

Leave a Comment