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

`std::shared_ptr`: is not a valid template type argument for parameter `_Ty`

本文关键字:quot 有效 类型参数 Ty std shared ptr 参数      更新时间:2023-10-16

>错误

Hero:未声明的标识符

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

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

attackInputgetSkillOS不是std::shared_ptr的成员

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

资料来源.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) {}
};
}

武器.h

#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);
}
};
}

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

您需要Hero.h的唯一原因Weapon.h.用于hit(...)功能。最好的解决方案是在其他地方声明命中函数。现在最好的地方是将其添加到Hero中。一个英雄有武器,用那个武器攻击另一个英雄,武器本身不能攻击,所以仅仅从语义上来说,在英雄职业中拥有命中功能更有意义。

您可以通过将代码拆分为标头 (.h( 和源文件 (.cpp( 并在Weaopon.h中向前声明Hero来编译它。但这并不能解决根本问题,设计仍然存在缺陷,如果扩展功能,可能会导致更多问题。