我正在尝试使用其他类的子类作为模板函数的参数:
template<typename T> class Class1{
public:
class Sub1{};
};
template<typename T> class Class2{
public:
Class2() = default;
template<typename T_other> void func(const typename Class1<T_other>::Sub1 sub) {}
};
// ...code
Class1<int>::Sub1 sub1;
Class2<float> class2;
class2.func( sub1); //ERROR
错误提示:
error: no matching function for call to ???Class2<float>::func(Class1<int>::Sub1&)???
class2.func( sub1); //ERROR
^
note: candidate: ???template<class T_other> void Class2<T>::func(typename Class1<T_other>::Sub1) [with T_other = T_other; T = float]???
template<typename T_other> void func(const typename Class1<T_other>::Sub1 sub) {}
^~~~
note: template argument deduction/substitution failed:
note: couldn't deduce template parameter ???T_other???
怎么了?错误消息指出了正确的类型(Class1<int>::Sub1
),那么为什么不能推断出它呢?