我正在使用libMesh FEM库并试图开发从libMesh继承的类(EqCore)。这个类将提供一些额外的功能,这些功能是由我想实际使用的类(MainEq)再次继承的。C++访问继承类的成员,其中继承的类是模板参数
两个函数set_constant和get_constant正在导致下面的错误。这些工作如不同的继承方案所示(见Inheritance of template class with a template member function in C++)。与这个问题的区别在于现在模板参数(Type)实际上是一个继承的类。这是一种危险的做法吗?
我希望得到这个代码工作或寻找替代方法的任何帮助。
错误信息:
In member function ‘void EqCore::set_constant(std::string, ParamType)’: test_libmesh.cpp:26:57: error: expected primary-expression before ‘>’ token
In member function ‘ParamType EqCore::get_constant(std::string)’: /home/slaughter/Documents/programs/source/test_libmesh.cpp:31:76: error: expected primary-expression before ‘>’ token
方案:
//! \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"));
}
Try'Type :: get_equ ation_systems()。parameters.template设置(名称)' –
2012-08-13 20:06:38
谢谢,这没有把戏。 – slaughter98 2012-08-13 20:20:14