温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c++ - `std::shared_ptr`: is not a valid template type argument for parameter `_Ty`
c++ shared-ptr

c++ - `std :: shared_ptr`:不是参数_Ty的有效模板类型参数

发布于 2020-03-28 23:32:58

失误

Hero:未声明的标识符

std::shared_ptrHero不是参数的有效模板类型参数_Ty

一元->std::shared_ptr未定义此运算符或未转换为预定义运算符可接受的类型

attackInputgetSkill不是会员std::shared_ptr

void my::Weapon::hit(std::shared_ptr,std::shared_ptr):无法将参数1从转换std::shard_ptr<my::Hero>std::shared_ptr

Source.cpp

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <windows.h>
#include "Hero.h"

void checkLife(my::Hero* hero)
{
    if (hero->is_dead())
    {
        delete hero;
        hero = nullptr;
    }
    if (hero == nullptr)
    {
        srand(time(0));
        int rand = 1 + std::rand() % 40;

        if (rand > 0 && rand <= 10) hero = new my::King();
        else if (rand > 10 && rand <= 20) hero = new my::Queen();
        else if (rand > 20 && rand <= 30) hero = new my::Troll();
        else if (rand > 30 && rand <= 40) hero = new my::Knight();
    }
}

int main()
{
    my::Hero* hero = nullptr;
    my::Hero* enemy = nullptr;

    while (true)
    {
        checkLife(hero);
        checkLife(enemy);

        hero->attackOutput(std::shared_ptr<my::Hero>(enemy));
        enemy->attackOutput(std::shared_ptr<my::Hero>(hero));

        system("cls");
        std::cout << hero->getName() << "`s health - " << hero->getHealth() << std::endl;
        std::cout << hero->getName() << "`s health - " << hero->getHealth() << std::endl;
        Sleep(1000);
    }

    if (hero != nullptr) delete hero;
    if (enemy != nullptr) delete enemy;
    system("pause");
    return 0;
} 

英雄

#pragma once
#include <memory>
#include <cstdlib>
#include <time.h>
#include <string>
#include "Weapon.h"

namespace my
{
    class Hero abstract
    {
    protected:
        std::shared_ptr<Weapon> weapon;
        std::string name;
        int health;
        int skill;
        int pressure;
        int nobleness;
        int beauty;

        virtual void attack(int damage)
        {
            srand(time(0));
            this->health -= damage + rand() % 20 - this->getPressurel();
        }
    public:
        Hero(std::string name, int health, int skill, int pressure, int nobleness, int beauty)
        {
            this->name = name;
            this->health = health;
            this->skill = skill;
            this->pressure = pressure;
            this->nobleness = nobleness;
            this->beauty = beauty;
        }
        std::string getName() const
        {
            return this->name;
        }
        int getHealth() const
        {
            return this->health;
        }
        int getSkill() const
        {
            return this->skill;
        }
        int getPressurel() const
        {
            return this->pressure;
        }
        int getNobleness() const
        {
            return this->nobleness;
        }
        int getBeauty() const
        {
            return this->beauty;
        }
        void attackInput(const int damage)
        {
            this->attack(damage);
        }
        void attackOutput(std::shared_ptr<Hero> enemy)
        {
            std::shared_ptr<Hero> thisHero(this);
            this->weapon->hit(std::shared_ptr<Hero>(thisHero), enemy);

        }
        virtual void takeWeapon(std::shared_ptr<Weapon> weapon)
        {
            this->weapon = weapon;
        }
        bool is_dead()
        {
            if (this->health <= 0) return true;
            else return false;
        }
    };

    class King : public Hero
    {
    public:
        King() : Hero("King", 300, 2, 4, 10, 15) {}
    };

    class Queen : public Hero
    {
    public:
        Queen() : Hero("Queen", 300, 2, 4, 10, 15) {}
    };

    class Troll : public Hero
    {
    public:
        Troll() : Hero("Troll", 300, 2, 4, 10, 15) {}
    };

    class Knight : public Hero
    {
    public:
        Knight() : Hero("Knight", 300, 2, 4, 10, 15) {}
    };
}

武器

#pragma once
#include "Hero.h"

namespace my
{
    class Weapon abstract
    {
    protected:
        const int damage;
        int wear;
    public:
        Weapon(int damage, int weight, int size, int wear) : damage(damage)
        {
            this->wear = wear;
        }
        Weapon() : Weapon(1, 1, 1, 1) {}

        virtual void setWeaponWear(int wear)
        {
            this->wear = wear;
        }
        virtual int getWeaponDamage() const
        {
            return this->damage;
        }
        virtual int getWeaponWear() const
        {
            return this->wear;
        }

        virtual void hit(std::shared_ptr<Hero> me, std::shared_ptr<Hero> enemy) = 0;
    };

    class Knife : public Weapon // NOZH
    {
    protected:
    public:
        virtual void hit(std::shared_ptr<Hero> me, std::shared_ptr<Hero> enemy)
        {
            int damage = this->damage * me->getBeauty();
            this->wear--;
            enemy->attackInput(damage);
        }
    };

    class Bow : public Weapon // LUCK
    {
    protected:
    public:
        virtual void hit(std::shared_ptr<Hero> me, std::shared_ptr<Hero> enemy)
        {
            int damage = this->damage * me->getNobleness();
            this->wear--;
            enemy->attackInput(damage);
        }
    };

    class Ax : public Weapon // TOPOR
    {
    protected:
    public:
        virtual void hit(std::shared_ptr<Hero> me, std::shared_ptr<Hero> enemy)
        {
            int damage = this->damage * me->getPressurel();
            this->wear--;
            enemy->attackInput(damage);
        }
    };

    class Sword : public Weapon // MECH
    {
    protected:
    public:
        virtual void hit(std::shared_ptr<Hero> me, std::shared_ptr<Hero> enemy)
        {
            int damage = this->damage * me->getSkill();
            this->wear--;
            enemy->attackInput(damage);
        }
    };
}

查看更多

查看更多

提问者
Alexander
被浏览
498
Nopileos 2020-01-31 19:02

正如@WhozCraig的评论中指出的那样,您具有循环依赖关系。您需要Weapon.hin Hero.hHero.hin Weapon.h

您需要Hero.h inWeapon.h . Is for thehit(...)function. The best solution would be to declare the hit function somewhere else. Right now the best place would be to add it to theHero` 的唯一原因一个英雄拥有一个武器,并且用另一个武器攻击该武器,一个武器本身不能攻击,因此仅从语义上讲,在英雄类中具有击中功能就更有意义。

您可以通过将代码分成头文件(.h)和源文件(.cpp)并具有Heroin 的前向声明来进行编译Weaopon.h但这不能解决潜在的问题,并且设计仍然存在缺陷,如果扩展功能,可能会引起更多问题。