C++中具有模板成员函数的模板类的继承
Inheritance of template class with a template member function in C++
>问题:我收到以下代码的以下错误,有人知道为什么吗?
问题:我正在研究一个类(ClassB),它控制来自外部库(libMesh)的许多类的行为。"...做点什么。。。部分代码旨在在这些具有模板函数的外部库类中设置一些变量。
我希望能够从继承类 (ClassC) 的构造函数中设置其中一些值。但是,如果我这样做,如下面的代码所示,我会看到显示的错误。如果我在构造函数中删除此命令,它可以正常工作。
我还包含一个更详细的示例,该示例使用产生相同的错误,但使用 libmesh 类本身,它说明了我想做得更好的地方。我不确定我正在尝试做的事情是否有用,我主要想知道为什么这不起作用,因为它似乎应该。
我找到了另一个类似的帖子,但我似乎无法将它们应用于我的问题。
模板继承内部类访问问题
感谢您的帮助,安德鲁
错误:
XXXXXXX@XXXXX:~/Documents/programs/build$ make test
[100%] Building CXX object CMakeFiles/test.dir/source/test.cpp.o
test.cpp: In constructor ‘ClassC<T>::ClassC()’:
test.cpp:16:29: error: expected primary-expression before ‘int’
test.cpp:16:29: error: expected ‘;’ before ‘int’
make[3]: *** [CMakeFiles/test.dir/source/test.cpp.o] Error 1
make[2]: *** [CMakeFiles/test.dir/all] Error 2
make[1]: *** [CMakeFiles/test.dir/rule] Error 2
make: *** [test] Error 2
简单代码:
// A class that sets that sets the value of something
template <typename Type> class ClassB{
public:
ClassB(){}
template<typename TypeValue> void set_value(TypeValue value){
// ... do something ...
}
};
// A class that inherits ClassB
template<typename T> class ClassC : public ClassB<T>{
public:
ClassC(){
// I want to do this (if I remove this it compiles)
ClassB<T>::set_value<int>(1);;
}
};
// The main function
int main (){
ClassC<double> c;
c.set_value<int>(1); // This works
}
问题特定的代码:
//! 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{
public:
// Class constructor
EqCore(EquationSystems& sys, string name) : eq_sys(sys){
// Creates a system that will store the constant(s)
name_.assign(name);
eq_sys.add_system<Type>(name_);
// I can set stuff from here
set_constant<double>("test4", 4);
}
// A function for storing a constant value
template<typename ParamType> void set_constant(std::string name, ParamType var){
eq_sys.parameters.set<ParamType>(name) = var;
}
// A function for retrieving a constant value
template<typename ParamType> ParamType get_constant(std::string name){
ParamType output = eq_sys.parameters.get<ParamType>(name);
return output;
}
// Reference to the controlling equation system
EquationSystems& eq_sys;
// The name of the system holding the constant(s)
string name_;
};
// A test class derived
template <typename Type> class EqBase : public EqCore<Type>{
public:
// Constructor
EqBase(EquationSystems& sys, string name) : EqCore<Type>(sys, name){
// I want to do this!
// (remove this and the associated print statement in the main and it works)
EqCore<Type>::set_constant<double>("test5", 5);
}
};
// 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: %fn", eq_sys.parameters.get<double>("test1"));
// Test EqBase/EqCore functions set/get functions
EqBase<TransientExplicitSystem> eq(eq_sys, "TestSystem");
eq.set_constant<double>("test2", 2);
printf("Test 2: %fn", eq.get_constant<double>("test2"));
// Test generic creation but accessed through EqBase
eq.eq_sys.parameters.set<double>("test3") = 3;
printf("Test 3: %fn", eq.eq_sys.parameters.get<double>("test3"));
// Test the constant created in EqCore constructor from EqBase
printf("Test 4: %fn", eq.eq_sys.parameters.get<double>("test4"));
// Test the constant created in EqBase constructor from EqBase
printf("Test 5: %fn", eq.eq_sys.parameters.get<double>("test5"));
}
编译器
无法弄清楚如何解析这个东西,因为它无法确定set_value是模板的名称。如果在::
后添加关键字template
,则可以解决问题:
ClassC(){
// I want to do this (if I remove this it compiles)
ClassB<T>::template set_value<int>(1);;
}
这个问题的答案非常详细地说明了原因:我必须在哪里以及为什么必须放置"模板"和"类型名称"关键字?
相关文章:
- 继承函数的重载解析
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 派生类调用使用非继承成员的继承函数
- 继承函数是否适用于 C++ 中的基类元素或派生类元素?
- 在链表中的某个点插入时出现问题,C++中的继承函数
- std :: is_same-来自integral_constant的继承函数的用例
- 如何将同名的继承函数视为重载函数
- 使用子函数的继承函数
- 执行覆盖的继承函数
- 相对于更专业的继承函数,模板更喜欢子类函数
- C++:继承函数并重新定义它们
- C++ 继承函数指针,可与派生方法一起使用
- 不同对象的向量,对象具有非继承函数
- C++多级继承函数调用
- 基类指向派生类继承函数调用的指针
- C++继承函数覆盖
- 派生类是否C++必须在头文件中包含继承函数/成员的定义
- 继承函数中的参数消失
- C++通过继承函数实现继承的抽象函数
- 构造函数和继承函数 c++