2

したがって、C ++の初心者で、他のボットクラスを継承するVBotという抽象クラスを使用しています。これで、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;
}

したがって、エラーは抽象クラスをインスタンス化できないため、CNNBotではなくVBotを構築しようとしていると思います。これは、出力で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();
}

あなたたちが与えることができるどんな助けでも、この初心者プログラマーに大いに感謝されるでしょう。

4

2 に答える 2

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");}
于 2012-10-24T23:18:55.467 に答える
0

Move()をオーバーライドせず、定義上、純粋仮想関数を持つクラスは抽象的であり、インスタンス化できません。体を動かしてください、そうすればあなたは元気になるはずです。

于 2012-10-24T23:19:57.450 に答える