I have a problem with templates - virtual function mismatch.
First of all, i have a parent class named Unit
and 2 child classes named Hero
and Monster
.
These 2 classes have both 2 subclasses and one of the class Hero's subclass is Crusader
.
class Unit {
template <typename target>
virtual void Attack(typename std::vector<target>::iterator targetPtr, std::shared_ptr<AttackSkill> skillPtr) {}
//ERROR (template function can not be virtual)
};
class Hero : public Unit {
template <typename target>
virtual void Attack(typename std::vector<target>::iterator targetPtr, std::shared_ptr<AttackSkill> skillPtr) {}
// ERROR (template function can not be virtual)
};
class Crusader : public Hero {
template <typename target>
void Attack(std::vector<target>::iterator targetPtr, std::shared_ptr<AttackSkill> skillPtr) {}
};
// Unit vector that contains heroes and monsters
std::vector<Unit> unitVector;
Crusader crusader1;
unitVector.at(0).emplace_back[crusader1];
The problem is, i want to access Attack
function from unitVector[0]
by writing virtual void Attack
functions to Hero
class and Unit
class
but unfortunately the C++ does not allow that.
What should I do to write these virtual functions?
Or can i use some generic pointers (if there is a such thing) instead of templates?
I want to use the attack function from different monsters and different heroes, so i have to use templates. If i try to write different functions such as attackHero
and attackMonster
one of them doesn't work since when i do that i'm basically trying to take an iterator of some non-existent class's vector.
Please help me out!