【发布时间】:2013-04-04 18:35:58
【问题描述】:
我正在尝试用 C++ 实现特定的模板设计,但遇到了一个我想要解决的问题。这是设置,并解释了要遵循的问题。
我声明一个模板化的抽象基类如下:
template <class TModel, class TVertex>
class AttributeEvaluator2f
{
public:
virtual void evaluate( TModel& model, TVertex& v, float x, float y ) = 0;
};
然后我想实现专门化模板参数之一的子类,如下所示:
template <class TVertex>
class SurfacePositionFromSphere : public AttributeEvaluator2f<Sphere3f,TVertex>
{
public:
virtual void evaluate( Sphere3f& sphere, TVertex& v, float theta, float phi )
{
sphere.SamplePosition( v.position, theta, phi );
};
};
然后我会像这样基于这些类实例化一个对象:
SurfacePositionFromSphere<BasicVertexDX11::Vertex> attribute();
编译错误表明我从未实现原始基类的抽象方法。这可能是因为在子类中,我将第一个参数直接声明为已经专用的方法(在这种情况下为Sphere3f)。
所以问题是,是否有可能以这种方式覆盖抽象基类方法参数的类型?如果没有,是否有类似的机制可用于提供类似的功能?
提前感谢您的帮助!
编辑:由于模板展开,确切的错误消息是巨大的,但这里是它的主要部分供您参考: 错误 C2259:“Glyph3::AttributeEvaluator2f”:无法实例化抽象 > 类 1> 与 1> [ 1> TModel=Glyph3::Sphere3f, 1> TVertex=Glyph3::BasicVertexDX11::Vertex 1>] 1> 由于以下成员: 1> 'void Glyph3::AttributeEvaluator2f::evaluate(TModel &,TVertex &,float,float)' : 是抽象的 1> 与 1> [ 1> TModel=Glyph3::Sphere3f, 1> TVertex=Glyph3::BasicVertexDX11::Vertex 1>] 1> c:\users\zij1fh\documents\visual studio 2012\projects\hg3\trunk\hieroglyph3\source\rendering\attributeevaluator2f.h(26) :参见“Glyph3::AttributeEvaluator2f::evaluate”的声明 1> 与 1> [ 1> TModel=Glyph3::Sphere3f, 1> TVertex=Glyph3::BasicVertexDX11::Vertex 1>] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : 请参阅对函数模板实例化 'void std::allocator<_ty>::construct<_objty>(_Objty *,_V0_t &&)' 正在编译 1> 与 1> [ 1> _Ty=Glyph3::AttributeEvaluator2f, 1> _Objty=字形3::AttributeEvaluator2f, 1> _V0_t=字形3::AttributeEvaluator2f 1>] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : 请参阅对函数模板实例化 'void std::allocator<_ty>::construct<_objty>(_Objty *,_V0_t &&)' 正在编译 1> 与 1> [ 1> _Ty=Glyph3::AttributeEvaluator2f, 1> _Objty=字形3::AttributeEvaluator2f, 1> _V0_t=字形3::AttributeEvaluator2f 1>] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(903) : 请参阅对函数模板实例化 'void std::allocator_traits<_alloc>::construct<_ty>(std ::allocator<_ty> &,_Objty *,_V0_t &&)' 正在编译
EDIT2:上面指出的用法不正确(正如 Andy 指出的那样)。这是我的确切用法:
VertexEvaluator2f< Sphere3f, BasicVertexDX11::Vertex > evaluator;
evaluator.Evaluators.push_back( SurfacePositionFromSphere<BasicVertexDX11::Vertex>() );
在 VertexEvaluator2f 类中,我遍历属性以生成一个顶点。为了完整起见,这里也是该类的声明:
template <class TModel, class TVertex>
class VertexEvaluator2f
{
public:
void SetModel( TModel& model ) {
m_Model = model;
};
void evaluate( TVertex& v, float x, float y ) {
for ( auto& evaluator : Evaluators ) {
evaluator.evaluate( m_Model, v, x, y );
}
};
std::vector< AttributeEvaluator2f< TModel, TVertex > > Evaluators;
protected:
TModel m_Model;
};
【问题讨论】:
-
您得到的确切错误信息是什么,关联的行是什么?设计应该可以工作 - 但您显示的声明不会实例化对象,而是声明一个名为
attribute的函数 -
这是一条非常长的错误消息,但我会在上面的描述中添加它...
-
那么错误是什么?
标签: c++ templates inheritance abstract-class