2012-07-21 28 views
7

Tôi đang cố triển khai giao diện trong C++ cho trò chơi mà tôi đang viết và tôi đang chạy trong một lỗi.Tôi nghĩ rằng tôi đã ghi đè một phương pháp ảo nhưng tôi vẫn nhận được: "X phải thực hiện phương pháp ảo thuần túy Y"

Dưới đây là giao diện và lớp con của nó mà tôi tạo ra:

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


}; 

Đây là lớp con:

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

Các lỗi được trả lại từ trình biên dịch là: Loại 'tấn công' phải triển khai phương pháp ảo thuần túy được thừa kế 'Attack :: calculate_damage'

Có ai biết cú pháp chính xác cho điều này không? Tôi có thiết lập kế thừa chính xác không?

+1

Khi bạn có C++ 11, hãy sử dụng từ khóa ghi đè khi bạn ghi đè. – PlasmaHH

Trả lời

7

const trong lớp học bị tước đoạt của bạn làm cho chữ ký của phương thức của lớp bị tước đoạt khác với phương pháp ảo trong lớp cơ sở. Theo như trình biên dịch, bạn đã gặp phải bị quá tải calculate_damage trong lớp Normal, cho nó một phiên bản constnon-const.

2

Bạn có thể tìm cho

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

Đó gọi các nhà xây dựng cơ sở, như bạn có thể mong đợi. Mã như nó bây giờ tạo ra một biến tạm thời chưa được đặt tên của loại Attack, đó là một lớp trừu tượng và những gì nó gây ra lỗi.

Ngoài ra, hàm cơ sở calculate_damage không phải là const.

Các vấn đề liên quan