c++ - 在 C++ 中的模板中调用模板时出错
问题描述
事件.h
class Event {
template <typename T, void(T::*MF)(int)>
void temp_callback(int r){
}
template <typename T, void(T::*MF)(int)>
void temp_add(T *obj, int a){
obj->testobj3<T, MF>(obj, a); // throws error here
temp_callback<T, MF>(a);
}
}
测试.h
class Test{
void testobj(int r);
void testobj2();
template <typename T, void(T::*MF)(int)>
void testobj3(T *obj, int a){
}
}
测试.cc
void Test::testobj2(){
Event eve;
eve.temp_add<Test, &Test::testobj>(this, 1);
}
int main() {
Test t;
t.testobj2();
}
上面的代码抛出一个错误,说“错误:','令牌之前的预期主表达式”。如果我改为为 testobj3 定义一个非模板函数并调用 obj->testobj3 (它在哪里抛出错误),它工作正常。不确定为什么模板化调用会引发错误?
非常感谢您提前抽出时间!
解决方案
推荐阅读
- laravel - 验证数字字符串具有固定数量的字符
- firebase - 在 for in 循环中等待异步函数
- python-3.x - DynamoDB 更新表达式是否允许条件表达式以更新表达式的特定组件为目标?
- react-native - React Native 平行四边形形状视图
- c# - 从一个 JSON 中提取数据并将其插入到 c# 中的另一个 JSON 中
- java - Spring Boot Maven 插件:目标和阶段
- r - 为什么我不能清理反应性数据(上传)并在返回绘图、表格等的其他函数中多次使用它?
- hadoop - 如何将数据集移动到本地 HortonWorks HDFS?
- json - 为什么字典对象不在我的模板中呈现表达式
- android - 在 Android 10 设备上从剪贴板获取复制的文本