c++ - 为什么这个没有声明任何纯虚成员函数的类是抽象的呢?

标签 c++ abstract-class virtual-functions pure-virtual pspsdk

下面的类 Game 是如何抽象的?我如何使其具体化以便创建它的实例?

游戏.h

#include <JApp.h>
#include <JGE.h>

class Game: public JApp
{    
private: 
    JGE* Engine;
    int x, y, x2, y2;
public:
    Game(JGE *engine);
    virtual ~Game();
    virtual void Create();
    virtual void Destroy();
    virtual void Update();
    virtual void Render();
};

主要.cpp

//Other headers
#include "game.h"
int main(void)
{
    JGE* engine = NULL;
    SetupCallbacks();
    engine = JGE::GetInstance();
    engine->printf("Starting Game!");
    Game* g = new Game(engine); // Error 'Game is an abstract type 
    engine->SetApp(g);
    engine->Run();
    engine->Destroy();

    sceKernelExitGame();
}

Game::Game(JGE* engine) : JApp(engine)
{
    Engine = engine;
    x = 0;
    x2 = 100;
    y = 0;
    y2 = 100;
}
void Game::Update()
{
    if (this->Engine->GetButtonClick(PSP_CTRL_UP))
    {
        x2 += 1;
        y2 += 1;

    }
    else if(this->Engine->GetButtonClick(PSP_CTRL_DOWN))
    {

        y2 += 10;
        y += 10;
    }
}
void Game::Create()
{    
}
void Game::Render()
{
     JRenderer* renderer = JRenderer::GetInstance();
     renderer->DrawLine(x, y, x2, y2, ARGB(0, 0, 0, 255));
}
Game::~Game()
{       
}
void Game::Destroy()
{   
}

附言任何解释都会有所帮助,因为我不是面向对象编程方面的专家。

这是错误信息和一些其他的东西:

1>------ Build started: Project: PSP Pong, Configuration: Debug Win32 ------
1>  psp-g++ -I. -Ic:/pspsdk/psp/sdk/include -O2 -G0 -Wall  -I. -Ic:/pspsdk/psp/sdk/include -O2 -G0 -Wall  -fno-exceptions -fno-rtti -D_PSP_FW_VERSION=150   -c -o main.o main.cpp
1>  main.cpp: In function 'int main()':
1>main.cpp(57): error : cannot allocate an object of abstract type 'Game'
1>  game.h (5) : note:   because the following virtual functions are pure within 'Game':
1>  c:/pspsdk/psp/sdk/include/JApp.h (78) : note:   virtual void JApp::Pause()
1>  c:/pspsdk/psp/sdk/include/JApp.h (84) : note:   virtual void JApp::Resume()
1>  main.cpp: In constructor 'Game::Game(JGE*)':
1>main.cpp(66): error : no matching function for call to 'JApp::JApp(JGE*&)'
1>  c:/pspsdk/psp/sdk/include/JApp.h (26) : note: candidates are: JApp::JApp()
1>  c:/pspsdk/psp/sdk/include/JApp.h (22) : note:                 JApp::JApp(const JApp&)
1>  c:\pspsdk\bin\make: *** [main.o] Error 1
1>  Press any key to continue . . . 
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.MakeFile.Targets(38,5): error MSB3073: The command "c:\pspsdk\bin\vsmake.bat" exited with code -1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

JApp 具有您必须在您的类中实现的纯虚函数。由于您尚未实现它们,因此您的类(class)不完整。

示例来说明问题:

struct B
{
    // this function is pure-virtual and
    // must be implemented in order to instantiate
    virtual void fn() = 0;
};

struct C : public B
{
    // missing implementation of fn().
    // as of now, this class is abstract because fn()
    // has no definition.
};

...

// try to instantiate...
C myobj;// this line produces error C2259: 'C' : cannot instantiate abstract class

关于c++ - 为什么这个没有声明任何纯虚成员函数的类是抽象的呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512004/

相关文章:

c++ - g++,创建用于分发的静态库

C# 抽象类继承

c++ - 重新分配指针如何影响多态

c++ - 是否需要 std::any_of 遵循短路逻辑?

c++ - 如何在 OSX 上使用 glfw3 在 OpenGL 和 OpenCL 之间创建共享上下文?

actionscript-3 - AS3-抽象类

c# - C# 中的抽象类与静态类

c# - 如果我不注意警告 "hides inherited member. To make the current member override that implementation...."怎么办

C++如何使虚函数返回任何类型的指针?

c++ - 将 armadillo 库添加到 linux 中的 g++ 编译器