-1

Here is Cell.cpp:

#include "Cell.h"

Cell::Cell(int x, int y) {
    this->x = x;
    this->y = y;
}

Here is my vector declaration:

std::vector<std::vector<Cell>> mazeGrid;

And here is where I try to set the vector at [i][j] to a cell object:

void Grid::generateCells() {
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            Cell temp(i, j);
            mazeGrid.at(i).at(j) = temp;
        }
    }
}

And the generateCells() method gives me this error: Severity Code Description Project File Line Suppression State Error (active) E1776 function "Cell::operator=(const Cell &)" (declared implicitly) cannot be referenced -- it is a deleted function Maze C:\Users\jbcal\source\repos\Maze\Maze\Grid.cpp 18

and

Severity Code Description Project File Line Suppression State Error C2280 'Cell &Cell::operator =(const Cell &)': attempting to reference a deleted function Maze C:\Users\jbcal\source\repos\Maze\Maze\Grid.cpp 18

Does anyone know how to do this correctly?

copy/paste code: Grid.h:

#pragma once
#ifndef GRID_H
#define GRID_H

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

class Grid {
public:

    /*
    holds the maze as a vector
    */
    std::vector<std::vector<Cell>> mazeGrid;

    /*
    constructor to make
    a maze grid with n rows and cols
    */
    Grid(int rows, int cols);

    /*
    draws all the cells
    */
    void Draw(sf::RenderWindow& window);

    void generateCells();

private:
    int rows;
    int cols;
};
#endif

Grid.cpp:

#include "Grid.h"

Grid::Grid(int rows, int cols) {
    // rows is always height / 50 (cellHeight)
    // cols is always width / 50 (cellWidth)
    this->rows = rows;
    this->cols = cols;
}

void Grid::Draw(sf::RenderWindow& window) {
    
}

void Grid::generateCells() {
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            Cell temp(i, j);
            mazeGrid.at(i).at(j) = temp;
            // why no worky :(
        }
    }
}

Cell.cpp:

#include "Cell.h"

Cell::Cell(int x, int y) {
    this->x = x;
    this->y = y;
}

Cell.h:

#pragma once
#ifndef CELL_H
#define CELL_H

#include <SFML/Graphics.hpp>

class Cell {
public:
    /*
    constructor to make a cell
    at row x and column y
    */
    Cell(int x, int y);

private:
    int x;
    int y;

    const int cellWidth = 50;
    const int cellHeight = 50;
    sf::RectangleShape cellRect;
};
#endif
jbcallv
  • 21
  • 5
  • 1
    It sounds like `Cell` is non-copyable, likely related to the [Rule of 3](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Cory Kramer Apr 12 '21 at 12:20
  • A "vector of vectors" is a bit of an anti-pattern to represent a grid by the way. Not horrible, but not great. – harold Apr 12 '21 at 12:24
  • 1
    Can you show a [mre] that anyone else can cut/paste ***exactly as shown*** and reproduce your compilation error? Also, all questions must include all relevant information ***in plain text***. Images are often unreadable and cannot be read by people with disabilities. Please remove all unreadable images and replace them with plain text. – Sam Varshavchik Apr 12 '21 at 12:24
  • @SamVarshavchik post has been reformatted. This should be reproducible. – jbcallv Apr 12 '21 at 12:29
  • 1
    @jbcallv Not it's not, your code is incomplete, for example, you don't provide the `Cell` implementation, which probably the cause of your issue. – Fareanor Apr 12 '21 at 12:31
  • 1
    Can you answer the following question: if I cut/paste ***exactly what's shown***, will I reproduce your compilation error, *exactly*? Unless you can confidently answer "yes", this is not a [mre]. – Sam Varshavchik Apr 12 '21 at 12:32
  • 1
    *Does anyone know how to do this correctly?* -- If you would have removed the `const` members from `Cell`, you may have seen that the code compiled, and then asked a more focused question concerning the `const` members. That's why you should have whittled the code down to a minimum *yourself* to isolate the problem, and then post a question concerning the isolated problem. – PaulMcKenzie Apr 12 '21 at 12:42
  • Moreover, you forgot to initialize `mazeGrid` in the `Grid` constructor. Calling the member function `generateCells()` will thow an exception from the very beginning since your `at()` calls will be all out of range. – Fareanor Apr 12 '21 at 12:51

1 Answers1

0

A few things, all related to this line of code:

mazeGrid.at(i).at(j) = temp;

This method returns a reference (A Cell &) to the given location. However, it won't create one, so if your i or j are out of the existing range, you'll get a runtime error. So you might have to add some vectors and some empty cells.

To make the above line work (assuming the cell in question already exists), you will need a copy operator on Cell:

class Cell {
...
public:
     Cell &operator=(const Cell &) {
     ...
     }

};

You could also implement a move operator, if you wanted.

The other way to do it would be:

Cell &cell = mazeGrid.at(i).at(j);
cell.x = x;
cell.y = y;

This, of course, depends on the implementation of Cell.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36