c++ - 错误 C2280 : attempting to reference a deleted function (trying to call vector. 删除)

标签 c++ vector compiler-errors deleted-functions

我是 C++ 新手。我正在使用 SFML 和 Box2D 开发 breakout-clone,编译时出现此错误。错误详情:

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 推送到 vector in

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);
        }
    }
}

问题是否与移动构造函数/赋值有某种关系?

最佳答案

由于 Tile 类有一个const 数据成员,因此它由编译器复制赋值运算符隐式定义为已删除

您必须自己显式定义复制赋值运算符。

关于c++ - 错误 C2280 : attempting to reference a deleted function (trying to call vector. 删除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24921149/

相关文章:

c++ - 如何仅使用Iterator显示Vector的前10个元素?

c++ - 如何使用 C++ 和 Boost Asio 从 HTTP post 请求获取键值

c++ - 根据索引值提取特定字节

c++ - 从c++ vector 中的指定索引中提取元素

c++ - "Process terminated with status -1073741819"带 vector 的简单程序

c++ - 如何从双向迭代器中减去 1

vector - 在 Rust 中制作堆栈(或其他可动态调整大小的类似矢量的东西)的正确方法是什么?

c++ - 具有固定维度和值的 vector (C++)

objective-c - 如何从一个.m文件中的另一个文件引用函数?

c++ - 在 VC++ 2010 中将数组作为参数传递时出现编译器错误