Reputation: 33
So im a beginner at C++ and i'm working with an abstract class called VBot in which i inherit to other bot classes. Now I know i need to override the pure virtual code in the VBot class which i do so i don't think that is the problem. I think because im so inexperienced at C++ I am doing some thing wrong with the constructors because i keep getting the cannot instantiate abstract class. This is the VBot.h file
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;
};
And this will be the 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;
}
So the error is the cannot instantiate a abstract class so i think it is trying to construct VBot and not CNNBot because in the output it is giving me void VBot::Move(void)' : is abstract and 'int VBot::EnergyToFightWith(void)' : is abstract
Sorry i forgot to add that part here is 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();
}
Whatever help you guys can give will be greatly appreciated to this novice programer.
Upvotes: 2
Views: 171
Reputation: 361585
To call a parent class's constructor you need to put it in the initialization list:
CNNBot( int startX, int startY, Panel ^ drawingPanel ):
VBot(startX, startY, drawingPanel)
{
image = gcnew Drawing::Bitmap("HappyBot.bmp");
}
You had this, which tried to create and then throw away a nameless VBot
object:
CNNBot( int startX, int startY, Panel ^ drawingPanel ){
VBot::VBot(startX,startY,drawingPanel);
image = gcnew Drawing::Bitmap("HappyBot.bmp");}
Upvotes: 1
Reputation: 452
Your not overriding Move() and by definition a class with any pure virtual functions is abstract and cannot be instantiated. Give a body to Move and you should be good.
Upvotes: 0