C ++访问继承类的成员,其中继承的类是模板参数

时间:2012-08-13 20:03:20

标签: c++ templates inheritance disambiguation

我正在使用libMesh FEM库,我正在尝试开发一个继承自libMesh的类(EqCore)。这个类将提供一些我想要实际使用的类(MainEq)再次继承的附加功能。

两个函数set_constant和get_constant导致下面的错误。这些工作如图所示,具有不同的继承方案(参见Inheritance of template class with a template member function in C++)。与此问题的区别在于,现在模板参数(Type)实际上是一个继承的类。这是一种危险的做法吗?

感谢您使用此代码或找到替代方法的任何帮助。

错误信息:

  

在成员函数'void EqCore :: set_constant(std :: string,ParamType)'中:   test_libmesh.cpp:26:57:错误:在'>'标记之前预期的primary-expression

     

在成员函数'ParamType EqCore :: get_constant(std :: string)'中:   /home/slaughter/Documents/programs/source/test_libmesh.cpp:31:76:错误:在'>'标记之前预期的primary-expression

方案:

//! \example test_libmesh.cpp

#include <string>
using std::string;

// libMesh includes
#include <libmesh.h>
#include <libmesh_common.h> 
#include <equation_systems.h>
#include <transient_system.h>
#include <explicit_system.h>
#include <parameters.h>
#include <mesh.h>
using namespace libMesh;

// Fundamental behavior that will be used among many classes
template <typename Type> class EqCore : Type{
    public:

        // Class constructor
        EqCore(EquationSystems& sys, string name) : Type(sys, name, 1){}

        // A function for storing a constant value (causes error)
        template<typename ParamType> void set_constant(std::string name, ParamType var){  
            Type::get_equation_systems().parameters.set<ParamType>(name) = var;
        }

        // A function for retrieving a constant value (causes error)
        template<typename ParamType> ParamType get_constant(std::string name){  
            ParamType output = Type::get_equation_systems().parameters.get<ParamType>(name);
            return output;
        } 
};

// A test class derived
class MainEq : public EqCore<ExplicitSystem>{
    public: 

        // Constructor
        MainEq(EquationSystems& sys) : EqCore(sys, "main"){ }   

};  


// Begin main function
int main (int argc, char** argv){

    // Initialize libMesh and create an empty mesh
    LibMeshInit init (argc, argv);
    Mesh mesh;

    // Test w/o any of the above classes
    EquationSystems eq_sys(mesh);
    eq_sys.parameters.set<double>("test1") = 1;
    printf("Test 1: %f\n", eq_sys.parameters.get<double>("test1"));

    // Test my class set/get functions
    MainEq eq(eq_sys);
    eq.set_constant<double>("test2", 2);
    printf("Test 2: %f\n", eq.get_constant<double>("test2"));   
}

2 个答案:

答案 0 :(得分:6)

因为您在模板内部,所以编译器无法在分析时自动确定set是一个模板,而且假设set是非模板,因此解析失败。 / p>

解决方案是明确告知编译器set是一个成员模板。

Type::get_equation_systems().parameters.template set<ParamType>(name) = var

答案 1 :(得分:2)

C ++模板元编程:来自Boost and Beyond的概念,工具和技术中,David Abrahams,Aleksey Gurtovoy(Amazon)解释如下:

double const pi = 3.14159265359;

template <class T>
int f(T& x)
{
    return x.convert<3>(pi);
}
  

T::convert可能是成员函数模板,在这种情况下   突出显示的代码将pi传递给convert<3>的特化。它   也可能成为数据成员,在这种情况下f返回   (x.convert < 3 ) > pi。这不是一个非常有用的计算,但是   编译器不知道它。

     

template关键字告诉编译器依赖名称是a   会员模板:

template <class T>
int f(T& x)
{
    return x.template convert<3>(pi);
}
  

如果省略template,编译器会认为x.convert没有   命名模板,&lt;随后将其解析为小于   操作

相关问题