我是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
当我尝试从std :: vector<中删除Tile对象时出现错误。瓦片GT; with vector.erase。
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);
}
}
}
问题是否与移动构造函数/赋值有关?
答案 0 :(得分:7)
由于Tile类具有 const数据成员,因此编译器复制assignmnet运算符隐式定义它被定义为已删除。
您必须自己定义复制赋值运算符显式。
答案 1 :(得分:0)
错误明确提到Tile &Tile::operator =(const Tile &)
,所以它是关于赋值运算符,而不是移动赋值运算符。您需要为Tile
实现它。你也缺少Tile
的拷贝构造函数。
引自here:
隐式声明或默认的复制赋值运算符 类T被定义为删除以下任何一种情况:
- T具有const
的非静态数据成员- T具有引用类型的非静态数据成员。
- T具有无法复制分配的非静态数据成员(已删除,无法访问或模糊的复制赋值运算符)
- T具有无法复制分配的直接或虚拟基类(已删除,不可访问或模糊的移动赋值运算符)
- T具有用户声明的移动构造函数
- T具有用户声明的移动赋值运算符