English 中文(简体)
模板专业的超大型基类方法
原标题:Overridig base class methods in template specialization

我有基级模范级和一米制衍生的等级,这是基级专业的模版。 基地班级有一个虚拟方法,I m试图超负荷上衍生的班级,我发现汇编错误。 事实上,我不敢肯定我这样做的方式是否正确。

template <class T>
struct BasicContext
{
  BasicContext() {}
  bool operator() (const T i) const
  {
    return isValid(i);
  }
  virtual bool isValid(const T) const
  {
    return true;
  }
  virtual BasicContext* Clone() const
  {
    return(new BasicContext<T>());
  }
};

class Myclass
{
 public:
 Myclass()
 {
 }
};
class MyclassContext : public BasicContext<Myclass*>
{
 public:
 MyclassContext();
 bool isValid(const Myclass* bdry) const override; 
 BasicContext* Clone() const override;
};

我的卷宗就是这样:

bool MyclassContext::isValid(const Myclass* m) const
{
  return true;
}
BasicContext<Myclass*>* MyclassContext::Clone() const
{
   return new MyclassContext();
}

我确实有以下汇编错误:

Severity Code Description Project File Line Suppression State Error C3668 MyclassContext::isValid : method with override specifier override did not override any base class methods DbgError F:C++DbgErrorDbgErrorDummy.h 36

基本上,瓦利德方法在衍生类别中不被视为基类方法。 我怎么能够凌驾基级方法,在具有专门参数类别的衍生类别中。 我指的是参数类型是什么?

问题回答

<>strong>problem 是:在您的基类isValid(const T)中,const上层,但在您的班次中,isValid(const Mynal* m), 您提供了低级>。

基本来说,顶级建筑群在压倒一切的方法时,必须保持顶级和低级建筑群的低水平。

Solution

因此,为了解决这一问题,将“条码”改为“条码” 如下所示:

//-------------------vvvvv---------------------->moved const here
bool isValid(Myclass*const bdry) const override; 

在<代码>isValid的申报和定义中确保你这样做。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?