继承、策略与模板混合
我想做如下的事情。
我有一堂课,其中有一些元素现在不是我们关心的。
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>’
I want to do something as the following.
I have a class as follows with some elements which are not our concern right now.
template<class A>
class Domain
{
};
My issue is, I want some new objects to be members of this class. But I cannot specify them in this definition. I have to specify it in some other file. My first thought was using inheritance as follows:
template<class G,class ctype>
class Data
{
public:
Data(G& g_);
protected:
std::vector<ctype> data1;
};
and then
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)
{
}
however, I've not been able to get it to compile.
Any suggestions how to go about this?
Any method on how to do this in a cleaner way?
the full program is the following. Its only in the header file. I haven't created an instance yet. The program is
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 }
and the errors are
../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>’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
A::ctype 被视为成员调用,而不是类型。您应该使用
typename
关键字:A::ctype is seen as a member call, not a type. You should use the
typename
keyword: