首页 > 解决方案 > 在 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 (它在哪里抛出错误),它工作正常。不确定为什么模板化调用会引发错误?

非常感谢您提前抽出时间!

标签: c++

解决方案


推荐阅读