嵌套类作为C ++中父类的模板参数 [英] Nested class as a template parameter of parent class in C++

查看:202
本文介绍了嵌套类作为C ++中父类的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将算法实现为从纯虚拟类派生的类,表示特定算法解决的问题类型。

I want to implement an algorithm as a class deriving from a pure virtual class representing the kind of problem the particular algorithm solves.

一般界面看起来像这样:

The general interface would look like this:

template<typename A, typename B>
class ISolutionToProblem
{
public:
    virtual void Init(const A & input, const B & param) = 0;
    virtual const B & ComputeSolution() = 0;

    virtual ~ISolutionToProblem() {}
};

并且实施将是例如:

template<typename T>
class MyAlgorithm:
    public ISolutionToProblem<typename MyAlgorithm<T>::WorkData, T>
{
public:
    struct WorkData { /* Stuff using T... */ };
    virtual void Init(const WorkData & input, const T & param);
    virtual const T & ComputeSolution();

virtual ~MyAlgorithm();
};

(更具体地说,问题实际上是寻路,但我不知道认为它是相关的)

我的问题是继承部分:我使用嵌套的结构作为模板参数,无论我尝试多么好与编译器交谈,它一直拒绝编译我的代码。

My problem is the inheritance part: I am using a nested struct as a template parameter, and no matter how nicely I try to talk to the compiler, it keeps refusing to compile my code.

我可能会变得很懒,只是将内部结构放在课堂之外,但如果可能的话我更喜欢它可以整齐地放在课堂上。

I could go lazy and just put the inner structure outside of the class, but if possible I'd prefer it to stay neatly placed in the class.


  1. 我正在尝试做的事情(在C ++ 98中)? / li>
  2. 如果是这样,我该怎么写呢? (奖励积分,如果你让我理解为什么语法不接受上面的表格)

  3. 否则,我做错了什么? (我的设计有缺陷吗?)






以下是编译器错误的方法看起来像。


Here is how the compiler error looks like.


  • g ++(4.8):
  • g++ (4.8):
error: no type named ‘WorkData’ in ‘class MyAlgorithm<int>’


  • clang(3.1):

  • clang (3.1):

    error: no type named 'WorkData' in 'MyAlgorithm<T>'


  • VS2012:

  • VS2012:

    error C2146: syntax error : missing ',' before identifier 'WorkData'
    see reference to class template instantiation 'MyAlgorithm<T>' being compiled
    error C2065: 'WorkData' : undeclared identifier
    error C2955: 'ISolutionToProblem' :
                 use of class template requires template argument list
    see declaration of 'ISolutionToProblem'


  • 推荐答案

    我认为你的问题是编译器在定义外部类和o之前不知道内部类是什么样的uter类定义为内部类作为模板参数。我不是百分百肯定这不能成功。 CRTP 就是一个类似于此的例子。

    I think your problem is the the compiler doesn't know what the inner class looks like until it has defined the outer class and the outer class is defined with the inner class as a template parameter. I'm not 100% sure this can't be made to work. The CRTP is an example similar to this that is known to work.

    模板可用于创建继承层次结构,但不应在层次结构的定义中使用。如果这听起来令人困惑,那是因为它是。继承和模板类不能很好地混合。请注意,即使CRTP使用继承和模板,它也不使用虚函数。

    Templates can be used to create inheritance hierarchies, but should not be use the in the definition of a hierarchy. If that sounds confusing, it's because it is. Inheritance and template classes don't mix well. Notice that even though CRTP uses inheritance and templates it does not use virtual functions.

    这篇关于嵌套类作为C ++中父类的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆