how to use virtual classes in c++と尋ねたことがあります。しかし、1人のユーザー(Emilio Garavaglia)は仮想クラスに似た何かのコードを追加する方法を紹介しました。しかし、私はコンパイルするために何をしているのか苦労しています。ここではコードです:インターフェイスから派生した別のクラスの中にあるインターフェイスから派生したクラスをインスタンシエートする...ちょっと調べるためには
global_defs.h
#define Interface class
#define abstract_class class
#define implements : public
I_Graphics.h
#ifndef I_GRAPHICS_H
#define I_GRAPHICS_H
#include <string>
#include "global_defs.h"
Interface I_Graphics
{
public:
virtual ~I_Graphics() {};
virtual void Initialize() = 0;
virtual void Frame() = 0;
virtual void Shutdown() = 0;
class I_Model;
virtual I_Model * CreateModel() = 0;
};
Interface I_Graphics::I_Model
{
public:
virtual ~I_Model() {}
virtual void Initialize(std::string const & filename, std::string const & textureFilename) = 0;
virtual void * GetVertexBuffer() = 0;
virtual void * GetIndexBuffer() = 0;
};
#endif
Graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "global_defs.h"
#include <map>
#include <string>
#include <memory>
#include "I_Graphics.h"
class Graphics implements I_Graphics
{
public:
Graphics();
~Graphics();
void Initialize();
void Frame();
void Shutdown();
class Model;
I_Model * CreateModel() {return new Model;} // <--- compile error here
private:
std::map <std::string, I_Model *> m_ModelList;
};
class Graphics::Model implements I_Graphics::I_Model
{
public:
Model();
~Model();
void Initialize(std::string filename, std::string textureFilename);
void * GetVertexBuffer();
void * GetIndexBuffer();
};
#endif
Graphics.cppここに行く 何も、本当にやりなさいまだ難しい部分で作業を始めましたが、モデルのインスタンス化が機能するようにしようとしました。そう
#include "Graphics.h"
Graphics::Graphics()
{
}
Graphics::~Graphics()
{
}
void Graphics::Initialize()
{
}
void Graphics::Frame()
{
}
void Graphics::Shutdown()
{
}
Graphics::Model::Model()
{
}
Graphics::Model::~Model()
{
}
void Graphics::Model::Initialize(std::string filename, std::string textureFilename)
{
}
void * Graphics::Model::GetVertexBuffer()
{
return NULL;
}
void * Graphics::Model::GetIndexBuffer()
{
return NULL;
}
少しコメントが言うように、私は言ってそこにエラーが発生します。
error C2512: 'Graphics::Model' : no appropriate default constructor available
を明らかgraphics.cppにおけるそれのコンストラクタがあるとき。コンパイラがここで不平を言っていることを誰かが説明できますか?
EDIT:
MSVCの小さな赤いくすみの上にマウスをかざすと、「抽象クラス型Graphics :: Modelのオブジェクトは使用できません」と表示されます。 ...純粋な仮想メンバーがないので、抽象的な権利ではないのですか?
EDIT:Castilhoの提案に
、私は以前のようにgraphics.hでたcreateModelを宣言したが、その後graphics.cppでそれを定義し、それははるかに特定のエラーをもたらしたが、私はまだ理由を理解していません。
error C2259: 'Graphics::Model' : cannot instantiate abstract class
1> due to following members:
1> 'void I_Graphics::I_Model::Initialize(const std::string &,const std::string &)' : is abstract
1> i_graphics.h(28) : see declaration of 'I_Graphics::I_Model::Initialize'
'#define abstract_class class'' #define implements:public 'なぜですか? –
習慣の力:P – FatalCatharsis