简体   繁体   English

我想我已经覆盖了一个虚方法,但我仍然得到:“X必须实现继承的纯虚方法Y”

[英]I think I've overridden a virtual method but I'm still getting: “X must implement the inherited pure virtual method Y”

I am attempting to implement an interface in C++ for a game that I am writing and I am running in an error. 我正在尝试在C ++中为我正在编写的游戏实现一个接口,而我正在运行一个错误。

Here is the interface and its subclass that I created: 这是我创建的接口及其子类:

//Attack.h
//defines a set of values associated with all attacks
//and an interface for all attacks
typedef unsigned const int attack_type;
typedef unsigned const int p_attack_type;
//defines the attack types
static const attack_type NORMAL = 0;
static const attack_type ADDER = 1;
static const attack_type MULTIPLIER = 2;
static const attack_type AREA_OF_EFFECT = 3;
static const attack_type DAMAGE_OVER_TIME = 4;
static const attack_type STATUS = 5;

//String representation of the names of attack types
static std::string attack_types [] = {"Normal","Adder","Multiplier","Area-Of-Effect","Damage-Over-Time","Status"};

class Attack
{
    protected:
        std::string attack_name;//the name of this attack
        attack_type type;//the type of attack this represents
        int cool_down_turns;//represents the number of turns this ability cannot be used after being used
        bool causesGlobalCooldown;//does this attack cause ALL abilities to cooldown -- during combat should force cooldown on everything
        bool on_cool_down;

    public:
        Attack(std::string name, attack_type, int cooldown_t, bool causesGlobalCooldown = false)
        {
            this->attack_name = name;
            this->type = attack_type;
            this->causesGlobalCooldown = causesGlobalCooldown;//whether or not ALL abilities need to cooldown
            this->cool_down_turns = cooldown_t;//amount of turns that cooldown occurs
            this->on_cool_down = false;//whether or not this attack is on cooldown
        }

        //the main method called to perform this attack
        //calculate damage, calculate cooldown -- for spells calculate cost
        //return the amount of damage this attack does
        virtual int do_attack(int modifier, int roll)
        {
            //calculate damage
            int damage = calculate_damage(modifier,roll);
            //check cooldown
            if(this->cool_down_turns>0)
            {
                this->cooldown(true);
            }
            return damage;
        }

        //defined by sub classes -- useful to tell you how much damage the attack is going to do
        virtual int calculate_damage(int modifier, int roll) = 0;

        //**standard method definitions**//

        //get the attack name
        std::string get_attack_name() const
        {
            return this->attack_name;
        }

        //get the attack type
        attack_type get_attack_type() const
        {
            return this->type;
        }

        //get the attack type name of this attack
        std::string get_attack_type_name() const
        {
            return attack_types[this->type];
        }

        //check the cooldown status of this attack
        bool cooldown() const
        {
            return on_cool_down;
        }

        //set the cooldown status of this attack
        void cooldown(bool set)
        {
            this->on_cool_down = set;
        }


};

Here is the subclass : 这是子类:

/*
 * Normal.cpp
 *
 *  Created on: Jul 15, 2012
 *      Author: christian
 *      Standard Definition of a Normal attack
 */
#include "Attack.h"

class Normal : public Attack
{
    public:

        Normal(std::string name)
        {
            Attack(name,NORMAL);
        }

        int calculate_damage(int modifier, int roll) const
        {
            return roll + modifier;
        }
};

The error being returned from the compiler is : The type 'Attack' must implement the inherited pure virtual method 'Attack::calculate_damage' 从编译器返回的错误是:类型'Attack'必须实现继承的纯虚方法'Attack :: calculate_damage'

Does anyone know the correct syntax for this? 有谁知道这个的正确语法? Do I have the inheritance setup correctly? 我是否正确设置了继承?

The const in your derrived class makes your derrived class's method's signature different from the virtual method in the base class. 您的derrived类中的const使得您的derrived类的方法的签名与基类中的virtual方法不同。 As far as the compiler is concerned you've overloaded calculate_damage in the Normal class, giving it a const and non-const version. 就编译器而言,你已经在Normal类中重载了 calculate_damage,给它一个constnon-const版本。

You are probably looking for 你可能正在寻找

Normal(std::string name)
  : Attack(name,NORMAL)
{}

That calls the base constructor, as you probably intended. 这可能会调用基础构造函数。 The code as it is now creates a temporary unnamed variable of type Attack , which is an abstract class and its what triggers the error. 现在的代码创建了一个类型为Attack的临时未命名变量,它是一个抽象类,它触发错误。

Also, the base function calculate_damage is not const . 此外,基函数calculate_damage不是const

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

相关问题 类型cv :: StereoBM必须实现继承的纯虚方法 - The Type cv::StereoBM must implement the inherited pure virtual method 类型a必须实现继承的纯虚方法b - The type a must implement the inherited pure virtual method b 类型“ wxMenuBar”必须实现继承的纯虚拟方法“ wxMenuBarBase :: GetLabelTop” - The type 'wxMenuBar' must implement the inherited pure virtual method 'wxMenuBarBase::GetLabelTop' 我应该如何使用派生类型作为参数实现纯虚方法? - How should I implement a pure virtual method with derived type as parameter? 如何使用私有继承的方法重写纯虚拟方法? - How can I override a pure virtual method using a privately inherited method? 公共虚方法被重写为继承类中的受保护 - Public virtual method overridden as protected in inherited class 如何覆盖纯虚拟方法,但仍强制子类实现相同的方法? - How can I override a purely virtual method, yet still force child classes to implement the same method? 如何实现纯虚拟析构函数? - How do I implement a pure virtual destructor? 试图编写一个纯虚方法,但是我显然语法错误? - Trying to write a pure virtual method but I apparently have the syntax wrong? 我可以重载基类中的纯虚方法吗? - Can I overload pure virtual method in the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM