虚拟功能不起作用



此代码未编译。基类中虚拟函数attack((中的所有问题。

它还没有访问大规模的课堂团队。我试着做这些课的朋友。但不管怎样,它都不起作用。我也做过函数ptr,但它不起作用。

虚拟函数在继承类中也不起作用。Visual studio 2015显示错误:

C2228、C2227、C2027。

请提供帮助。

class Team;
class Unit
{
protected:
int hp;
int dmg;
int doodge;
public:
Unit(int hp, int dmg, int doodge): hp(hp), dmg(dmg), doodge(doodge){}
int GetHP()
{
return hp;
}
void SetHP(int hp)
{
this->hp = hp;
}
virtual void attack(Team &T)
{
int id = rand() % 3;
for (int i = 0; i < 3; i++)
if (typeid(*this) == typeid(T.arr[i]))
{
id = i;
break;
}
if (T.arr[id] <= 0)
return;
else
T.arr[id]->SetHP(T.arr[id]->GetHP() - this->dmg);
}
};
class Swordsman:public Unit
{
public:
Swordsman():Unit(15,5,60){}
//virtual void attack(Team & T)override
//{
//  int id = rand() % 3;
//  for (int i = 0; i < 3; i++)
//      if (typeid(Swordsman) == typeid())
//      {
//          id = i;
//          break;
//      }
//  if (*T.arr[id]->GetHP <= 0)
//      return;
//  else
//      *T.arr[id]->SetHP(T.arr[id]->GetHP() - dmg);
//}
};
class Archer :public Unit
{
public:
Archer() :Unit(12, 4, 40) {}
//virtual void attack(Team & T)override
//{
//  int id = rand() % 3;
//  for (int i = 0; i < 3; i++)
//      if (typeid(Archer) == typeid())
//      {
//          id = i;
//          break;
//      }
//  if (*T.arr[id]->GetHP <= 0)
//      return;
//  else
//      *T.arr[id]->SetHP(T.arr[id]->GetHP() - dmg);
//}
};
class Mage :public Unit
{
public:
Mage() :Unit(8, 10, 30) {}
/*virtual void attack(Team & T)override
{
int id = rand() % 3;
for (int i = 0; i < 3; i++)
if (typeid(*this) == typeid())
{
id = i;
break;
}*/
};
class Team
{
static short counter;
string name;
Unit* arr[3];
public:
Team()
{
name = "Team " + counter++;
for (int i = 0; i < 3; i++)
{
int selecter = rand() % 3;
switch (selecter)
{
case 0:
arr[i] = new Swordsman();
break;
case 1:
arr[i] = new Archer();
break;
case 2:
arr[i] = new Mage();
break;
}
}
}
~Team()
{
delete[]arr;
}
Unit * ptr(int id)
{
return arr[id];
}
bool check()
{
bool res = false;
for (int i = 0; i < 3; i++)
if (arr[i]->GetHP() > 0)
res = true;
return res;
}
void print()
{
cout << endl << "tt" << name << endl << endl;
cout << "t" << typeid(*arr[0]).name() << endl;
cout << "t" << typeid(*arr[1]).name() << endl;
cout << "t" << typeid(*arr[2]).name() << endl;
}
friend class Unit;
};
short Team::counter = 0;
class Game
{
Team A, B;
public:
int Play()
{
while (true)
{
A.ptr(1)->attack(B);
if (A.check())
return 1;
else if (B.check())
return 2;
}
}
};
int main()
{
return 0;
}

忽略任何无关内容:

class Team;
class Unit
{
public:
virtual void attack(Team &T)
{
if(typeid(*this) == typeid(T.arr[i]))
//                           ^^^
{ }
}
};

您正在访问类Team的成员,但在给定的时间,您只提供了Team的声明。。。附带说明:这并不特定于虚拟函数,但会发生在您编写的任何代码中。

现在的问题是TeamUnit这两个类的函数实现都依赖于另一个类的完整定义。因此,这个问题的唯一解决方案是在类之外实现其中一个函数,例如:

class Team;
class Unit
{
public:
// requires Team, so only declared, not implemented!
virtual void attack(Team &T);
//                          ^
};
class Team
{
// complete definition!
};
void Unit::attack(Team& t)
{
// now implementation of...
}

另一个小问题是arr成员是私有的。好吧,您已经提供了一个getter(ptr(,所以使用它(并给它一个更好的名称…(

如果你想进一步实现干净的设计,可以将你的单元和团队划分为不同的编译单元,每个单元都有一个标题和一个源文件:

单位.h:

class Team;
class Unit
{
// private members
public:
// only declarations as above, including constructor/destructor
// by the way: you are lacking a virtual destructor!!!
virtual ~Unit();
};

单位.cpp:

#include "unit.h"
#include "team.h" // fetch the definition of Team!
Unit(/*...*/) { }
Unit::~Unit() { }
// function definitions as shown above...

对于Team,甚至您的Unit派生类以及Game类,也可以执行同样的操作。不过,请注意,如果您想继承,您需要完整的类定义,因此您需要在标头中包含unit.h:

射手h:

#include "unit.h"
class Archer : public Unit
{
// again, only function declarations...
// as base class has already a virtual destructor, own destructor
// gets virtual implicitly (even the default one), so if you do
// not need it, you do not have to define it...
};

archer.cpp:

#include "archer.h"
// and whatever else needed, solely, unit.h already comes with archer.h
// implementations...

相关内容

最新更新