无法实例化抽象类我认为我搞砸了我的构造函数C ++

时间:2012-10-24 23:15:18

标签: visual-studio-2010 visual-c++

所以我是C ++的初学者,我正在使用一个名为VBot的抽象类,其中我继承了其他的bot类。现在我知道我需要覆盖VBot类中的纯虚拟代码,我这样做,我不认为这是问题。我认为因为我对C ++缺乏经验,所以我对构造函数做了一些错误,因为我不断实现抽象类的实例化。 这是VBot.h文件

class VBot
{
public:

    VBot( int startX, int startY, Panel ^ drawingPanel ) : 
      x(startX), y(startY), panel(drawingPanel), energy(100), image(NULL) { };

virtual ~VBot() { };


virtual void Move() = 0;

virtual int EnergyToFightWith() = 0;

bool IsDead() const { return energy <= 0; }

virtual void Show();

bool CollidedWith ( VBot * b ) const;

void DoBattleWith ( VBot * b );

protected:
     int x, y;                           // Current position of the VBot
     gcroot<Drawing::Bitmap ^> image;    // Image displayed for the VBot
     gcroot<Panel ^> panel;              // Panel on which to show the VBot.
     int energy                          // Current energy of the VBot

};
class CNNBot : public VBot
{
public:
CNNBot( int startX, int startY, Panel ^ drawingPanel ){
    VBot::VBot(startX,startY,drawingPanel);
    image = gcnew Drawing::Bitmap("HappyBot.bmp");}
~CNNBot(){};

void Move();

int EnergyToFightWith();
bool IsDead() { return (VBot::IsDead()); }
virtual void Show() { VBot::Show();}
bool CollidedWith ( VBot * b ) { return VBot::CollidedWith(b);}
void DoBattleWith ( VBot * b ){ VBot::DoBattleWith(b);}

private:
static const int MOVE_VAL = 55;
static const int RIGHT_BOUND = 490;
static const int DOWN = 40;
static const int MAXY = 379;
bool switcher;

};

这将是VBot.cpp

#include "stdafx.h"     

#include "Vbot.h"

void VBot::Show()
{ 
  Graphics ^ g = panel->CreateGraphics();
  g->DrawImageUnscaled( image, x, y );
  g->~Graphics();
}


bool VBot::CollidedWith ( VBot * b ) const
{
if (  b == NULL )
  return false;

return   ( x + image->Width ) >= b->x
     && ( b->x + b->image->Width ) >= x
     && ( y + image->Height ) >= b->y
     && ( b->y + b->image->Height ) >= y;

}


void VBot::DoBattleWith ( VBot * b )
{
   int mine = EnergyToFightWith();
   int yours = b->EnergyToFightWith();
   if( mine == yours )
{
   energy = energy - mine / 2;
   b->energy = b->energy - yours / 2;
}
else if ( mine > yours )
{
   if ( b->energy > 1 )
   {
      b->energy = b->energy - yours;
      energy = energy + yours / 2;
   }
   else
   {
      b->energy = b->energy - 1;
      energy = energy + 1;
   }
}
else
{
    if ( energy > 1 )
    {
       energy = energy - mine;
       b->energy = b->energy + mine / 2;
    }
    else
    {
       b->energy = b->energy + 1;
       energy = energy - 1;
    }
  }
}
int CNNBot::EnergyToFightWith()
{
return this->energy;
}

所以错误是无法实例化一个抽象类,所以我认为它试图构建VBot而不是CNNBot,因为在输出中它给了我 void VBot :: Move(void)':是抽象的 和 'int VBot :: EnergyToFightWith(void)':是抽象的

对不起,我忘了在这里添加那部分是Move()

 void CNNBot::Move()
{
if (this->switcher)
{
    this->x += MOVE_VAL;
    if( this->x >= RIGHT_BOUND)
    {
        this->x = 0;
        this->y += DOWN;
        if(this->y > MAXY)
        {
            this->switcher = false;
            this->y = MAXY;
        }
    }
}
else
{
    this->x += MOVE_VAL;
    if( this->x >= RIGHT_BOUND)
    {
        this->x = 0;
        this->y -= DOWN;
        if(this->y < 0)
        {
            this->switcher = true;
            this->y = 0;
        }
    }
}
panel->Invalidate();
}

无论你们给予什么帮助,我都将非常感谢这位新手。

2 个答案:

答案 0 :(得分:1)

要调用父类的构造函数,需要将其放在初始化列表中:

CNNBot( int startX, int startY, Panel ^ drawingPanel ):
    VBot(startX, startY, drawingPanel)
{
    image = gcnew Drawing::Bitmap("HappyBot.bmp");
}

你有这个,试图创建然后扔掉一个无名的VBot对象:

CNNBot( int startX, int startY, Panel ^ drawingPanel ){
    VBot::VBot(startX,startY,drawingPanel);
    image = gcnew Drawing::Bitmap("HappyBot.bmp");}

答案 1 :(得分:0)

您不重写Move(),并且根据定义,具有任何纯虚函数的类是抽象的,无法实例化。给一个身体移动,你应该是好的。