-4

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

I get an "vector subscript is out of range" error.

Edit: here's my mre hope it helps

Edit: It says that "vector subscript is out of range." Does that help?

Edit: I edited the code. It's COMPLETELY different, but it gives the same error and I'm pretty sure it's the same thing. If you solve this, you'll solve the other problem.

tictactoe.cpp:

#include "engine.h"

int main()
{
    Engine engine;
    while (engine.getRunning() == true)
    {
        engine.input();
        engine.update();
        engine.draw();
    }
    return 0;
}

engine.h

#include "grid.h"

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

    bool getRunning()
    {
        return running;
    }

    // input

    void update()
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
            {
                window.close();
                running = false;
            }
            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
                    sf::Vector2f mousePositionFloat(static_cast<float>(mousePosition.x),
                        static_cast<float>(mousePosition.y));
                    const sf::FloatRect& node1 = grid.getNode(0, 0);
                    if (node1.contains(mousePositionFloat))
                    {
                        window.close();
                    }
                }
            }
        }
    }
    // draw
};

grid.h:

#include <vector>
#include <SFML/Graphics.hpp>

class Grid
{
private:
    std::vector<sf::FloatRect> nodes;
    std::vector<std::vector<int>> grid;
    sf::Vector2f currPos;
    float nodeWidth, nodeHeight;
    int nodesPerRow;
    int currentRow, currentColumn;

public:
    Grid(float width, float height, int perRow) :
        currPos(0.f, 0.f), nodeWidth(width), nodeHeight(height),
        nodesPerRow(perRow), currentRow(0), currentColumn(0) {}

    const sf::FloatRect& getNode(int row, int col) const {
        int index = row * nodesPerRow + col;
        return nodes[index];
    }
};

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.

Edit: I don't know why the subscript would be out of range.

  • 3
    please post a [mcve] and the compiler error message. Do you really have that loops right in the definition of the class? – 463035818_is_not_an_ai Aug 24 '23 at 19:13
  • 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? – Elijah Campbell Aug 24 '23 at 19:14
  • 2
    the code you posted will cause a totally different error https://godbolt.org/z/9feTf47nM. Please read about [mcve]. 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 – 463035818_is_not_an_ai Aug 24 '23 at 19:15
  • 3
    `getBricks` returns a copy of the vector so the iterators you're using point to completely different containers. Return a reference instead. – Retired Ninja Aug 24 '23 at 19:20
  • 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. – user4581301 Aug 24 '23 at 19:31
  • Why does a *{single}* brick class contain a vector of bricks? When does this recursion stop? – Thomas Matthews Aug 24 '23 at 19:55
  • Retired Ninja's thing helped. – Elijah Campbell Aug 24 '23 at 19:57
  • I would expect a `Wall` to have a container of `Brick`. A brick should be a single brick; that's what most reality says. – Thomas Matthews Aug 24 '23 at 19:57
  • Does the `isAlive` member apply to the Brick or to the container? – Thomas Matthews Aug 24 '23 at 19:59
  • So I should name bricks wall then? – Elijah Campbell Aug 24 '23 at 20:00
  • Yeah I also put in the deleteBricks() function `bricks[rowIndex][colIndex].isAlive = false;` but that was before I got it working so I couldn't test it. – Elijah Campbell Aug 24 '23 at 20:02
  • No, do not rename, as you will the same issue: A `brick` will contain zero or more `Wall`, which contain zero or more brick, which contain zero or more wall, .... Move the container out of the `Brick` class. Declare `std::vector> wall;`, which is a 2d vector of bricks, which makes more sense. I recommend you *draw* the `Brick` class, where `Brick` contains a vector of `Brick`, which contains a vector of `Brick`. Let me know when this ends. :-( – Thomas Matthews Aug 24 '23 at 20:05

0 Answers0