【问题标题】:inheritance, policy mixed with templates继承,策略与模板混合
【发布时间】:2011-09-20 05:24:25
【问题描述】:

我想做如下的事情。

我有一个类如下,其中包含一些我们现在不关心的元素。

template<class A>
class Domain
{

};

我的问题是,我希望一些新对象成为此类的成员。但我不能在这个定义中指定它们。我必须在其他文件中指定它。我的第一个想法是使用继承如下:

template<class G,class ctype>
class Data
{
  public:
     Data(G& g_);
  protected:
     std::vector<ctype> data1;
};

然后

template<class A>
class Domain : public Data<A,A::ctype>
{
    public:
       Domain(A& a);
       void writeData(data1);
};
template<class A>
Domain<A>::Domain(A& a)
{

}

但是,我无法编译它。 有什么建议可以解决这个问题吗? 关于如何以更清洁的方式执行此操作的任何方法?

完整的程序如下。它只在头文件中。我还没有创建实例。该程序是

28   template<class GV, class Data>
29   class System : public Data<GV,GV::ctype>
30   {
31     private:
32      typedef Dune::VTKWriter<GV> VTKWriter;
33      GV& gv_;
34      VTKWriter vtkwriter_;
35  
36    public:
37      System(GV& gv);  
38      void writeSystemInfo(std::string outfile);
39   };
40  
41   template<class GV, class Data>
42   System<GV,Data>::System(GV& gv) : gv_(gv) , vtkwriter_(gv)
43   {
44   }
45 
46   template<class GV,class Data>
47   void System<GV,Data>::writeSystemInfo(std::string outfile)
48   {
49     Data::addDatatoVTK();
50     vtkwriter_.write(outfile, Dune::VTKOptions::binaryappended);
51   }

错误是

../dune/simulationlab/system/system.hh:29:29: error: expected template-name before ‘<’ token
../dune/simulationlab/system/system.hh:29:29: error: expected ‘{’ before ‘<’ token
../dune/simulationlab/system/system.hh:29:29: error: expected unqualified-id before ‘<’ token
../dune/simulationlab/system/system.hh:46:33: error: invalid use of incomplete type ‘class Dune::System<GV, Data>’
../dune/simulationlab/system/system.hh:29:9: error: declaration of ‘class Dune::System<GV, Data>’
../dune/simulationlab/system/system.hh:52:60: error: invalid use of incomplete type ‘class Dune::System<GV, Data>’
../dune/simulationlab/system/system.hh:29:9: error: declaration of ‘class Dune::System<GV, Data>’

【问题讨论】:

  • 我不想要多个域类声明。
  • writeData(data1); 这行应该是函数调用还是函数声明?因为它也不是。此外,您的构造函数定义与声明不匹配,并且到处都缺少分号。
  • 对不起....我写的有点匆忙...实际上程序太长了我试图缩短它以突出我会修复的关键点...跨度>
  • 编写一个您认为应该编译但没有编译的最小程序,并将它与您遇到的编译器错误一起发布。否则,这个问题是无法回答的。
  • 我希望它现在没有错误

标签: c++ templates inheritance policy


【解决方案1】:

A::ctype 被视为成员调用,而不是类型。您应该使用typename 关键字:

template<class A>
class Domain : public Data<A,typename A::ctype>
{
    public:
       Domain(A& a);
};

【讨论】:

  • 在您的最新更新中,我看到您尝试在模板本身中实例化作为 typename 过去的东西的模板实例。我不认为这种形式的递归模板是允许的(或定义了行为)。
猜你喜欢
  • 2011-12-02
  • 1970-01-01
  • 2013-02-13
  • 2018-07-02
  • 2019-01-06
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多