Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Данный код в классе Race считает target необъявленным идентификатором. Исправь его. Код:…

  1. User

    Данный код в классе Race считает target необъявленным идентификатором. Исправь его. Код: class Race { protected: string race;//Название расы string ability;//название способности int racephysicalvalue;//Бонус к физ урону int racemagicvalue;//Бонус к маг урону int racehpvalue;//Бонус к hp int racempvalue;//Бонус к mp public: Race(const string& race, const string& ability, int racephysicalvalue, int racemagicvalue, int racehpvalue, int racempvalue) : race(race), ability(ability), racephysicalvalue(racephysicalvalue), racemagicvalue(racemagicvalue), racehpvalue(racehpvalue), racempvalue(racempvalue) {}

    string getrace() {
        return race;
    }
    
    string getability() {
        return ability;
    }
    
    virtual void useraceability(Character* target) = 0;
    
    int getrpv() {
        return racephysicalvalue;
    }
    
    int getrmv() {
        return racemagicvalue;
    }
    
    int getrhp() {
        return racehpvalue;
    }
    
    int getrmp() {
        return racempvalue;
    }
    virtual ~Race() {}
    

    };

    class Human :public Race { public: Human(): Race("Человек","",0,0,0,0) {} void useraceability(Character* target) override {} ~Human(){} };

    class Orc :public Race { public: Orc(): Race("Орк", "Исступление", 5, -10, 20, -10) {}

    void useraceability(Character* target) override {
        target->takeDamage(10);
        target->attackbonus = 5;
    }
    
    ~Orc(){}
    

    };

    class Elf :public Race { public: Elf() : Race("Эльф", "Восстановление маны", 0, 10, -10, 10) {}

    void useraceability(Character* target) override {
        target->restoreMana(20 + racemagicvalue);
    }
    
    ~Elf(){}
    

    };

    class Undead :public Race { public: Undead() : Race("Нежить", "Восстановление ", -5, 5, -20,5 ) {}

    void useraceability(Character* target) override {
    
    }
    
    ~Undead(){}
    

    };

    class Demon :public Race { public: Demon() : Race("Демон", "Огненная плеть", 10, -5, 0, -5) {}

    void useraceability(Character* target) {
    
    }
    
    ~Demon(){}
    

    }; //Место для класса trait

    class Character { protected: string name; Race* race; string specialization; string trait;//Черты + изъяны int hp; int mp; public: Character(const string& name, Race* race, const string& specialization, const string& trait, int maxhp, int maxmp) : name(name), race(race), specialization(specialization), trait(trait), hp(maxhp+race->getrhp()), mp(maxmp+race->getrmp()) {}

    int maxhp = hp;
    int maxmp = mp;
    int attackbonus = 0;
    virtual void useAbility(Character* target) = 0;
    
    virtual void useAbility(Mob* target) = 0;
    
    bool isalive() {
        return hp >= 0;
    }
    
    void status() {
        cout << "Имя: " << getName() << endl;
        cout << "Раса: " << race->getrace() << endl;
        cout << "Класс: " << specialization << endl;
        cout << "Черта: " << trait << endl;
        cout << "HP: " << hp << "/" << maxhp << endl;
        cout << "MP: " << mp << "/" << maxmp << endl;
    }
    
    virtual void attack(Character* target) = 0;
    
    virtual void attack(Mob* target) = 0;
    
    void takeDamage(int damage) {
        hp -= damage;
        cout << name << " получает " << damage << " урона" << endl;
    }
    
    void restoreHealth(int amount) {
        if (!isalive())
            return;
        if (hp + amount > maxhp)
            hp = maxhp;
        else
            hp += amount;
        cout << name << " восстанавливает " << amount << " здоровья" << endl;
    }
    
    void useMana(int amount) {
        mp -= amount;
        cout << name << " использует " << amount << " маны" << endl;
    }
    
    void restoreMana(int amount) {
        if (!isalive())
            return;
        if (mp + amount > maxmp)
            mp = maxmp;
        else
        mp += amount+race->getrmp();
        cout << name << " восполняет " << amount << " маны" << endl;
    }
    
    string getName(){
        if (!isalive())
            return name + "(мёртв)";
        return name;
    }
    
    virtual ~Character(){}
    };
    

    class Knight : public Character { public: Knight(const string& name, Race* race, const string& trait, int hp, int mp) : Character(name, race, "Рыцарь", trait, hp, mp) {}

    void useAbility(Character* target) override {
        if (isalive()) {
            if (mp >= 30) {
                useMana(30);
                cout << "Способность : Удар Щитом" << endl;
                target->takeDamage(20+race->getrpv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName()<<endl;
    }
    
    void useAbility(Mob* target) override {
        if (isalive()) {
            if (mp >= 30) {
                useMana(30);
                cout << "Способность : Удар Щитом" << endl;
                target->takeDamage(20 + race->getrpv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void attack(Mob* target) override {
        if (isalive()) {
            cout << name << " рубит мечом " << target->getName() << endl;
            target->takeDamage(50 + race->getrpv());
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    void attack(Character* target) override {
        if (isalive()) {
            cout << name << " рубит мечом " << target->getName() << endl;
            target->takeDamage(50 + race->getrpv());
        }
        else
            cout << name<<" мёртв и не может атаковать"<<target->getName() << endl;
    }
    
    ~Knight(){}
    

    };

    class Cleric : public Character { public: Cleric(const string& name, Race* race, const string& trait, int hp, int mp) : Character(name, race, "Клирик", trait, hp, mp){}

    void useAbility(Character * target) override {
        if (isalive()) {
            if (mp >= 20) {
                useMana(20);
                cout << "Способность : Лечение" << endl;
                target->restoreHealth(30 + race->getrmv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    } 
    
    void useAbility(Mob* target) override {
        if (isalive()) {
            if (mp >= 20) {
                useMana(20);
                cout << "Способность : Лечение" << endl;
                target->restoreHealth(30 + race->getrmv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void attack(Character* target) override {
        if (isalive()) {
            cout << name << " тыкает жезлом " << target->getName() << " в глаз" << endl;
            target->takeDamage(5);
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    void attack(Mob* target) override {
        if (isalive()) {
            cout << name << " тыкает жезлом " << target->getName()<< " в глаз" << endl;
            target->takeDamage(5);
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    ~Cleric(){}
    

    };

    class Paladin : public Character { public: Paladin(const string& name, Race* race, const string& trait, int hp, int mp) : Character(name, race, "Паладин", trait, hp, mp) {}

    void useAbility(Character* target) override {
        if (isalive()) {
            if (mp >= 40) {
                useMana(40);
                cout << "Способность : Удар Света" << endl;
                target->takeDamage(40+race->getrmv() + race->getrpv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void useAbility(Mob* target) override {
        if (isalive()) {
            if (mp >= 40) {
                useMana(40);
                cout << "Способность : Удар Света" << endl;
                target->takeDamage(40 + race->getrmv() + race->getrpv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void attack(Character* target) override {
        if (isalive()) {
            cout << name << " размахивается и бьёт молотом " << target->getName() << endl;
            target->takeDamage(40 + race->getrpv());
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    void attack(Mob* target) override {
        if (isalive()) {
            cout << name << " размахивается и бьёт молотом " << target->getName() << endl;
            target->takeDamage(40 + race->getrpv());
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    ~Paladin(){}
    

    };

    class Mage : public Character { public: Mage(const string& name, Race* race, const string& trait, int hp, int mp) : Character(name, race, "Маг", trait, hp, mp) {}

    void useAbility(Character* target) override {
        if (isalive()) {
            if (mp >= 40) {
                useMana(40);
                cout << "Способность : Огненный Шар" << endl;
                target->takeDamage(50+race->getrmv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void useAbility(Mob* target) override {
        if (isalive()) {
            if (mp >= 40) {
                useMana(40);
                cout << "Способность : Огненный Шар" << endl;
                target->takeDamage(50 + race->getrmv());
            }
            else {
                cout << "Не хватает маны" << endl;
            }
        }
        else
            cout << name << " мёртв и не может использовать способности на " << target->getName() << endl;
    }
    
    void attack(Character* target) override {
        if (isalive()) {
            cout<<name<<" бьёт посохом " << target->getName() << endl;
            target->takeDamage(10+race->getrpv());
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    void attack(Mob* target) override {
        if (isalive()) {
            cout << name << " бьёт посохом " << target->getName() << endl;
            target->takeDamage(10+race->getrpv());
        }
        else
            cout << name << " мёртв и не может атаковать" << target->getName() << endl;
    }
    
    ~Mage(){}
    

    };

  2. Assistant

    В коде отсутствует объявление класса Character. Необходимо его добавить:

    class Character;
    
    class Race {
    protected:
        string race;//Название расы
        string ability;//название способности
        int racephysicalvalue;//Бонус к физ урону
        int racemagicvalue;//Бонус к маг урону
        int racehpvalue;//Бонус к hp
        int racempvalue;//Бонус к mp
    public:
        Race(const string& race, const string& ability, int racephysicalvalue, int racemagicvalue, int racehpvalue, int racempvalue)
            : race(race), ability(ability), racephysicalvalue(racephysicalvalue), racemagicvalue(racemagicvalue), racehpvalue(racehpvalue), racempvalue(racempvalue) {}
    
        string getrace() {
            return race;
        }
        
        string getability() {
            return ability;
        }
    
        virtual void useraceability(Character* target) = 0;
    
        int getrpv() {
            return racephysicalvalue;
        }
    
        int getrmv() {
            return racemagicvalue;
        }
    
        int getrhp() {
            return racehpvalue;
        }
    
        int getrmp() {
            return racempvalue;
        }
        virtual ~Race() {}
    };
    
    class Character {
    protected:
        string name;
        Race* race;
        string specialization;
        string trait;//Черты + изъяны
        int hp;
        int mp;
    public:
        Character(const string& name, Race* race, const string& specialization, const string& trait, int maxhp, int maxmp)
            : name(name), race(race), specialization(specialization), trait(trait), hp(maxhp+race->getrhp()), mp(maxmp+race->getrmp()) {}
        
        int maxhp = hp;
        int maxmp = mp;
        int attackbonus = 0;
        virtual void useAbility(Character* target) = 0;
    
        virtual void useAbility(Mob* target) = 0;
        
        bool isalive() {
            return hp >= 0;
        }
    
        void status() {
            cout << "Имя: " << getName() << endl;
            cout << "Раса: " << race->getrace() << endl;
            cout << "Класс: " << specialization << endl;
            cout << "Черта: " << trait << endl;
            cout << "HP: " << hp << "/" << maxhp << endl;
            cout << "MP: " << mp << "/" << maxmp << endl;
        }
    
        virtual void attack(Character* target) = 0;
    
        virtual void attack(Mob* target) = 0;
    
        void takeDamage(int damage) {
            hp -= damage;
            cout << name << " получает " << damage << " урона" << endl;
        }
    
        void restoreHealth(int amount) {
            if (!isalive())
                return;
            if (hp + amount > maxhp)
                hp = maxhp;
            else
                hp += amount;
            cout << name << " восстанавливает " << amount << " здоровья" << endl;
        }
    
        void useMana(int amount) {
            mp -= amount;
            cout << name << " использует " << amount << " маны" << endl;
        }
    
        void restoreMana(int amount) {
            if (!isalive())
                return;
            if (mp + amount > maxmp)
                mp = maxmp;
            else
            mp += amount+race->getrmp();
            cout << name << " восполняет " << amount << " маны" << endl;
        }
    
        string getName(){
            if (!isalive())
                return name + "(мёртв)";
            return name;
        }
    
        virtual ~Character(){}
    };
    
    ...
    
    
  3. User

    Теперь ошибка "использование неопределенного типа Character"

  4. Assistant

    Проблема заключается в том, что класс Character используется в определении метода useraceability(Character* target) в классе Race, но объявление класса Character происходит позже.

    Чтобы исправить ошибку, нужно добавить объявление класса `Character