C++ 对象在for循环中的二维矢量插入
以下是Cell.cpp:C++ 对象在for循环中的二维矢量插入,c++,vector,C++,Vector,以下是Cell.cpp: #include "Cell.h" Cell::Cell(int x, int y) { this->x = x; this->y = y; } 这是我的矢量声明: std::vector<std::vector<Cell>> mazeGrid; 第h单元: #pragma once #ifndef CELL_H #define CELL_H #include <SFML/Graphi
#include "Cell.h"
Cell::Cell(int x, int y) {
this->x = x;
this->y = y;
}
这是我的矢量声明:
std::vector<std::vector<Cell>> mazeGrid;
第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
#pragma一次
#ifndef单元
#定义单元
#包括
类单元{
公众:
/*
构造一个单元格的构造函数
在第x行和第y列
*/
单元(intx,inty);
私人:
int x;
int-y;
const int cellWidth=50;
const int cellHeight=50;
sf::矩形形状cellRect;
};
#恩迪夫
一些事情都与这一行代码有关:
mazeGrid.at(i).at(j) = temp;
此方法返回给定位置的引用(单元格&)。但是,它不会创建一个,因此,如果i或j超出现有范围,则会出现运行时错误。因此,您可能需要添加一些向量和一些空单元格
要使上述行正常工作(假设相关单元格已存在),您需要在单元格上设置复制运算符:
class Cell {
...
public:
Cell &operator=(const Cell &) {
...
}
};
如果需要,还可以实现移动操作符
另一种方法是:
Cell &cell = mazeGrid.at(i).at(j);
cell.x = x;
cell.y = y;
当然,这取决于单元格的实现。这听起来像是
单元格
是不可复制的,可能与您是否可以显示其他人可以完全按照所示进行剪切/粘贴并复制您的编译错误有关?此外,所有问题必须以纯文本形式包含所有相关信息。图像通常无法阅读,残疾人无法阅读。请删除所有不可读的图像,并用纯文本替换它们。@jbcallv不是,你的代码不完整,例如,你没有提供单元格
实现,这可能是你的问题的原因。你能回答以下问题吗:如果我剪切/粘贴显示的内容,我会重现你的编译错误吗,确切地除非你能自信地回答“是”,否则这不是一个问题。有人知道如何正确地做到这一点吗?--如果您想从单元格
中删除常量
成员,您可能已经看到代码已编译,然后询问了一个关于常量
成员的更为集中的问题。这就是为什么你应该自己将代码缩减到最低限度来隔离问题,然后发布一个关于隔离问题的问题。
#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
mazeGrid.at(i).at(j) = temp;
class Cell {
...
public:
Cell &operator=(const Cell &) {
...
}
};
Cell &cell = mazeGrid.at(i).at(j);
cell.x = x;
cell.y = y;