指针类型的类模板专门化调用中没有匹配的函数
No matching function for call in class template specialization for a pointer type
我不知道为什么不能将指针作为引用传递给函数。也许我没有注意到错误的关键。
class Point{
public:
Point(){}
};
template<typename KEY,typename VALUE>
class TemplTest{
public:
TemplTest(){}
bool Set(const KEY& key,const VALUE& value){
return false;
}
};
template<typename KEY,typename VALUE>
class TemplTest<KEY*,VALUE>{
public:
TemplTest(){}
bool Set(KEY*& key,const VALUE& value){
return true;
}
};
int main(){
Point p1;
TemplTest<Point*,double> ht;
double n=3.14;
ht.Set(&p1,n);
return 0;
}
错误:no matching function for call to 'TemplTest<Point*, double>::Set(Point*, double&)'
no known conversion for argument 1 from 'Point*' to 'Point*&'
请帮忙,谢谢!
因为引用不能绑定到右值,所以&p1
是一个没有名称的右值,以绕过这个
Point *p1_ptr = &p1;
Point *&p1_ptr_ref = p1_ptr;
ht.Set( p1_ptr_ref, n);
或者您可以将const
添加到键
bool Set( KEY* const& key,const VALUE& value){
// ^^^^^
return false;
}
相关文章:
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 专门化模板覆盖函数/避免对象切片
- 如何使用模板化类专门化模板化函数?
- 线程 std::调用未知类型,无法专门化函数错误
- 如何在编译时专门化大型模板函数中的小部分
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 从 std 命名空间中专门化函数模板的想法有多糟糕?
- 为模板函数专门化一个模板类
- 基于参数值的c++模板成员函数专门化
- 如何确定函数专门化的主模板
- 等价参数的c++函数专门化
- 基于运行时值调用不同的模板函数专门化
- 跨库的模板函数专门化符号匹配
- 模板函数专门化,c++
- 如何使用type_traits或模板函数专门化来整合模板方法
- 如何在子类中重载模板化函数(专门化)
- 可变模板函数:专门化头/尾和空基准情况
- 不允许void的部分函数专门化-替代解决方案