首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个没有声明任何纯虚拟成员函数的类是抽象的?

为什么这个没有声明任何纯虚拟成员函数的类是抽象的?
EN

Stack Overflow用户
提问于 2011-12-15 05:35:45
回答 1查看 360关注 0票数 2

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

game.h

代码语言:javascript
复制
#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();
};

main.cpp

代码语言:javascript
复制
//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()
{   
}

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

以下是错误消息以及其他一些内容:

代码语言:javascript
复制
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 ==========
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-15 05:37:29

JApp具有必须在类中实现的纯虚拟函数。因为您还没有实现它们,所以您的类是不完整的。

示例来说明这个问题:

代码语言:javascript
复制
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
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8512004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档