c++ - 我想我已经重写了一个虚拟方法,但我仍然得到 : "X must implement the inherited pure virtual method Y"

标签 c++ eclipse inheritance gcc virtual

我正在尝试用 C++ 为我正在编写的游戏实现一个接口(interface),但我运行时出错。

这是我创建的接口(interface)及其子类:

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


};

这是子类:

/*
 * 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;
        }
};

编译器返回的错误是: “Attack”类型必须实现继承的纯虚方法“Attack::calculate_damage”

有人知道正确的语法吗?我的继承设置是否正确?

最佳答案

派生类中的 const 使派生类的方法签名不同于基类中的虚方法。就编译器而言,您在 Normal 类中重载 calculate_damage,给它一个 constnon-const 版本。

关于c++ - 我想我已经重写了一个虚拟方法,但我仍然得到 : "X must implement the inherited pure virtual method Y",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11595096/

相关文章:

c++ - c++ 方法内部的异常

c++ - 如何在 C++ 中将 "new"用于多边形

Eclipse 无法在 4.3.2 中创建 Groovy 类

c++ - Eclipse C++ 构建问题

javascript - MDN 对 __proto__ 属性的解释可能有错误?

java - java中的静态方法和实例方法-父类和子类

android - Android View 的 ID 可以在多个 Activity 之间安全共享吗?

c++ - 在进程中隔离和多重实例化 C 库

c++ - 如何在不借助 std::function 的情况下存储函数对象?

java - Eclipse 自定义 java 编译器