gpt4 book ai didi

c++ - 实例化一个派生自接口(interface)的类,该接口(interface)位于另一个派生自接口(interface)的类中......只需阅读即可找出答案

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:32:35 25 4
gpt4 key购买 nike

我刚才问过how to use virtual classes in c++ ,令我沮丧的是,我了解到你不能。但是一位用户(即“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

图形.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

图形.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 中显然有它的构造函数时。有人可以解释编译器在这里提示什么吗?

编辑:
不确定它是否意味着什么,但是当鼠标悬停在 MSVC 中的红色小波浪线时,它说,“不允许抽象类类型 Graphics::Model 的对象”。 ...但它没有任何纯虚拟成员,所以它不是抽象的,对吗?

编辑:
在 Castilho 的建议下,我像以前一样在 graphics.h 中声明了 CreateModel,但随后在 graphics.cpp 中定义了它,它产生了一个更具体的错误,但我仍然不明白为什么。

error C2259: 'Graphics::Model' : cannot instantiate abstract class<br/>
1> due to following members:<br/>
1> 'void I_Graphics::I_Model::Initialize(const std::string &,const std::string &)' : is abstract<br/>
1> i_graphics.h(28) : see declaration of 'I_Graphics::I_Model::Initialize'

最佳答案

您在定义模型类之前使用它。在单独的 CPP 中定义函数 CreateModel,它可能会起作用。

关于c++ - 实例化一个派生自接口(interface)的类,该接口(interface)位于另一个派生自接口(interface)的类中......只需阅读即可找出答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10273227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com