Visual studio 2010 不能实例化抽象类,我想我搞砸了我的构造函数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; };

Visual studio 2010 不能实例化抽象类,我想我搞砸了我的构造函数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; };,visual-studio-2010,visual-c++,Visual Studio 2010,Visual C++,所以错误是无法实例化抽象类,所以我认为它试图构造VBot而不是CNNBot,因为在输出中它给了我 void VBot::Move(void)“”:是抽象的 及 'int VBot::EnergyToFightWith(void)':是抽象的 对不起,我忘了在这里添加一部分是Move() void CNNBot::Move() { 如果(此->开关) { 此->x+=移动值; 如果(此->x>=右边界) { 这个->x=0; 此->y+=向下; 如果(此->y>最大值) { 此->切换器=错误;

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

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

void CNNBot::Move()
{
如果(此->开关)
{
此->x+=移动值;
如果(此->x>=右边界)
{
这个->x=0;
此->y+=向下;
如果(此->y>最大值)
{
此->切换器=错误;
这->y=最大值;
}
}
}
其他的
{
此->x+=移动值;
如果(此->x>=右边界)
{
这个->x=0;
这->y-=向下;
如果(此->y<0)
{
此->切换器=真;
这->y=0;
}
}
}
面板->失效();
}

无论你们能提供什么样的帮助,我们都将非常感谢这位编程新手。

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

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");}

根据定义,不重写Move()和具有任何纯虚函数的类是抽象的,不能实例化。让身体活动一下,你应该会很好。

非常感谢你现在回到计划的其余部分。
CNNBot( int startX, int startY, Panel ^ drawingPanel ):
    VBot(startX, startY, drawingPanel)
{
    image = gcnew Drawing::Bitmap("HappyBot.bmp");
}
CNNBot( int startX, int startY, Panel ^ drawingPanel ){
    VBot::VBot(startX,startY,drawingPanel);
    image = gcnew Drawing::Bitmap("HappyBot.bmp");}