我是 C++ 新手。我正在使用 SFML 和 Box2D 开发突破克隆,编译时出现此错误。错误详情:
c:\program files (x86)\visual studio express 2013\vc\include\xutility(2420): error C2280: 'Tile &Tile::operator =(const Tile &)' : attempting to reference a deleted function
c:\users\harry\documents\visual studio 2013\projects\cpp-breakout\cpp-breakout\entities\tile.hpp(27) : compiler has generated 'Tile::operator =' here
当我尝试使用 vector.erase 从 std::vector< Tile> 中擦除 Tile 对象时出现错误。
Tile.h
class Tile : public Entity {
public:
Tile(b2World* world, float posX, float posY);
void draw(sf::RenderWindow& window);
const float PTM_RATIO = 32.f;
const int HALF_WIDTH = 32;
const int HALF_HEIGHT = 16;
int armor;
bool flaggedToErase = false;
b2Body* tileBody;
sf::Sprite sprite;
};
Tile.cpp
Tile::Tile(b2World* world, float posX, float posY)
{
// Define a body.
b2BodyDef tileBodyDef;
tileBodyDef.type = b2_staticBody;
tileBodyDef.position.Set((posX + HALF_WIDTH) / PTM_RATIO, (posY + HALF_HEIGHT) / PTM_RATIO);
// Use the body definition to create the actual body instance.
tileBody = world->CreateBody(&tileBodyDef);
tileBody->SetUserData(this);
// Define shape.
b2PolygonShape tileShape;
tileShape.SetAsBox(HALF_WIDTH / PTM_RATIO, HALF_HEIGHT / PTM_RATIO);
// Define fixture.
b2FixtureDef tileFixtureDef;
tileFixtureDef.shape = &tileShape;
tileFixtureDef.density = 10.0f;
tileFixtureDef.restitution = 0.1f;
tileFixtureDef.friction = 0.0f;
//tileFixtureDef.isSensor = true;
bUserData* bud = new bUserData;
bud->entityType = TILE;
tileFixtureDef.userData = bud;
// Create fixture.
tileBody->CreateFixture(&tileFixtureDef);
}
我将 Tiles 推到向量中
Level.cpp
std::vector<Tile> solidTiles;
void Level::loadLevel(b2World* world) {
// Loop through the map and set the tile position and sprites to the tiles.
for(unsigned int i = 0; i < map.size(); i++) {
for(unsigned int j = 0; j < map[i].size(); j++) {
// Set sprites to the tiles in every grid cell which is not -1,-1.
if(map[i][j].x != -1 && map[i][j].y != -1) {
Tile tempTile(world, j * TILE_WIDTH, i * TILE_HEIGHT);
tempTile.sprite = tiles;
tempTile.sprite.setOrigin(sf::Vector2f(HALF_WIDTH, HALF_HEIGHT));
tempTile.sprite.setTextureRect(sf::IntRect(map[i][j].x * TILE_WIDTH, map[i][j].y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT));
tempTile.sprite.setPosition(sf::Vector2f(tempTile.tileBody->GetPosition().x * PTM_RATIO, tempTile.tileBody->GetPosition().y * PTM_RATIO));
solidTiles.push_back(tempTile);
}
}
}
}
我试着擦掉里面的瓷砖
PlayState.cpp
void PlayState::removeSprites() {
for(unsigned int i = 0; i < level1.solidTiles.size(); i++) {
if(level1.solidTiles[i].flaggedToErase == true) {
level1.solidTiles.erase(level1.solidTiles.begin() + i);
}
}
}
问题是否与移动构造函数/赋值有关?