简体   繁体   English

二进制“==”:未找到采用“Enemy”类型左侧操作数的运算符(或没有可接受的转换)

[英]binary '==': no operator found which takes a left-hand operand of type 'Enemy' (or there is no acceptable conversion)

I'm making a game and I'm trying to find enemies with the bool shouldDie == true.我正在制作一个游戏,我正在尝试使用 bool shouldDie == true 找到敌人。 I have an Enemy std::list and personally I have no idea what's wrong with the code.我有一个 Enemy std::list,我个人不知道代码有什么问题。 If an enemy has shouldDie == true I'll just have an animation playing.如果敌人有 shouldDie == true 我只会播放动画。 Hopefully you can help me understand why I get the error.希望您能帮助我理解为什么我会收到错误消息。

Also I have no overloaded == operator, I've searched online and I'm not sure if it's necessary...此外,我没有重载 == 运算符,我在网上搜索过,但不确定是否有必要...

bool foundEnemy(Enemy& enemy)
{
    return enemy.shouldDie == true;
}
void Enemy::PlayDeathAnimation(std::list<Enemy>& enemies)
{
    dead = true;

    auto it = std::find_if(enemies.begin(), enemies.end(), foundEnemy); // where I think the error is
    auto enemy = std::next(it, 0);

    if (animationClock.getElapsedTime().asSeconds() >= 0.05f)
    {
        enemy->icon.setTextureRect(animationFrames[animationCounter]);
        if (animationCounter >= 8)
        {
            enemies.remove(*enemy);
        }
        animationCounter++;
        animationClock.restart();

    }
}
class Enemy : public Entity
{
public:
    Enemy() {}
    Enemy(sf::Vector2f position, sf::Texture* texture,Player* target);
    ~Enemy();

    void Update(sf::RenderWindow* window, float tElapsedTime);
    void Draw(sf::RenderWindow* window);
    void Fire(float tElapsedTime);
    void CheckBullets();
    void CheckEnemyBullets();
    void CheckHealth();

    void SetPosition(float x, float y);

    bool shouldDie = false;

    void PlayDeathAnimation(std::list<Enemy>& enemies);



private:

    bool dead = false;

    sf::Texture* deathSpriteSheet;
    std::vector<sf::IntRect> animationFrames;

    std::vector<Bullet> bullets;
    sf::RectangleShape origin;
    sf::RectangleShape aim;
    Player* target;

    int animationCounter = 0;
    sf::Clock animationClock;

};

The error is actually not where you think it is, but few lines below.错误实际上不在您认为的位置,而是在下面几行。

if (animationCounter >= 8)
{
    enemies.remove(*enemy); // here
}

You are using std::list::remove function, which will search for any elements of the list matching given element and removes those.您正在使用std::list::remove函数,它将搜索与给定元素匹配的列表中的任何元素并删除它们。 To know which element is the same as given, it needs to know how to compare them, thus the need for operator ==要知道哪个元素与给定的元素相同,它需要知道如何比较它们,因此需要operator ==

Use std::list::erase() instead - this function accepts an iterator and will remove exact element you point to.改用std::list::erase() - 此函数接受一个迭代器并将删除您指向的确切元素。

if (animationCounter >= 8)
{
    enemies.erase(enemy); // no dereference of the iterator
}

Side note - compiler is very useful tool.旁注 - 编译器是非常有用的工具。 If it detects an error, it will point you to a direct line and column where it occurred, although sometimes this piece of information is very well hidden in a ton of other (less useful) prints.如果它检测到错误,它会将您指向发生错误的直接行和列,尽管有时这条信息很好地隐藏在大量其他(不太有用的)打印件中。

If you don't understand the language of the compiler (yet), you can copy and paste the whole error message into your SO question, this will help us diagnose error faster.如果您还不了解编译器的语言(还),您可以将整个错误消息复制并粘贴到您的 SO 问题中,这将帮助我们更快地诊断错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 binary&#39;==&#39;:找不到带有&#39;std :: string&#39;类型的左手操作数的运算符(或者没有可接受的转换) - binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion) C2678二进制&#39;==&#39;:找不到使用&#39;Card&#39;类型的左操作数的运算符(或者没有可接受的转换) - C2678 binary '==': no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion) 错误C2678:二进制'==':找不到哪个运算符采用类型的左操作数(或者没有可接受的转换) - error C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion) 二进制&#39;=&#39;:未找到采用&#39;_Ty&#39;类型的左操作数的运算符(或没有可接受的转换) - binary '=': no operator found which takes a left-hand operand of type '_Ty' (or there is no acceptable conversion) 错误C2678:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;std :: istream&#39;类型的左操作数的运算符(或没有可接受的转换) - error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 错误C2678:二进制'=':找不到哪个运算符带有'const Recipe'类型的左手操作数(或者没有可接受的转换) - error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion) 错误1错误C2678:二进制&#39;!=&#39;:未找到采用&#39;std :: ofstream&#39;类型的左操作数的运算符(或没有可接受的转换) - Error 1 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::ofstream' (or there is no acceptable conversion) 错误 C2678:二进制“=”:未找到采用“const std::string”类型的左操作数的运算符(或没有可接受的转换) - error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) 错误C2678:二进制'<<':找不到运算符,它接受类型为'const std :: ofstream'的左手操作数(或者没有可接受的转换) - Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion) 错误C2678:二进制&#39;+&#39;:未找到采用&#39;volatile A&#39;类型的左操作数的运算符(或者没有可接受的转换) - error C2678: binary '+': no operator found which takes a left-hand operand of type 'volatile A' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM