Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继承、策略与模板混合_C++_Templates_Inheritance - Fatal编程技术网

C++ 继承、策略与模板混合

C++ 继承、策略与模板混合,c++,templates,inheritance,C++,Templates,Inheritance,我想做如下的事情 我有一节课,下面是一些我们现在不关心的元素 template<class A> class Domain { }; 模板 类域 { }; 我的问题是,我希望一些新对象成为这个类的成员。但我不能在这个定义中具体说明它们。我必须在其他文件中指定它。我的第一个想法是使用继承,如下所示: template<class G,class ctype> class Data { public: Data(G& g_); protecte

我想做如下的事情

我有一节课,下面是一些我们现在不关心的元素

template<class A>
class Domain
{

};
模板
类域
{
};
我的问题是,我希望一些新对象成为这个类的成员。但我不能在这个定义中具体说明它们。我必须在其他文件中指定它。我的第一个想法是使用继承,如下所示:

template<class G,class ctype>
class Data
{
  public:
     Data(G& g_);
  protected:
     std::vector<ctype> data1;
};
模板
类数据
{
公众:
数据(G&G);
受保护的:
std::矢量数据1;
};
然后

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)
{

}
模板
类域:公共数据
{
公众:
域名(A&A);
无效写入数据(数据1);
};
样板
域::域(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   }
28模板
29类系统:公共数据
30   {
31私人:
32 typedef沙丘::VTKWriter VTKWriter;
33全球价值和全球价值;
34录像机录像机录像机;
35
36公众:
37系统(GV和GV);
38无效写入系统信息(标准::字符串输出文件);
39   };
40
41模板
42系统::系统(GV和GV):GV_(GV),vtkwriter_(GV)
43   {
44   }
45
46模板
47无效系统::writeSystemInfo(标准::字符串输出文件)
48   {
49 Data::addDatatoVTK();
50 vtkwriter_u2;.write(输出文件,沙丘::VTKOptions::二进制附加);
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>’

。/dune/simulationlab/system/system.hh:29:29:错误:“A::ctype”之前的预期模板名称被视为成员调用,而不是类型。您应该使用
typename
关键字:

template<class A>
class Domain : public Data<A,typename A::ctype>
{
    public:
       Domain(A& a);
};
模板
类域:公共数据
{
公众:
域名(A&A);
};

我不想要类域的多个声明。是行
writeData(data1)应该是函数调用还是函数声明?因为它也不是。此外,构造函数定义与声明不匹配,并且到处都缺少分号。对不起。。。。我写得有点匆忙。。。实际上,这个程序太长了,我试图缩短它以突出关键点,我会解决这个问题…编写一个你认为应该编译但没有编译的最小程序,并将它与你得到的编译器错误一起发布。否则,这个问题是不可能回答的。我希望它现在不包含任何错误。在您最近的更新中,我看到您尝试在模板本身中实例化某个过去作为typename的模板实例。我认为这种形式的递归模板是不允许的(或者行为是被定义的)。