没有匹配函数来调用"std::less:<int>:less(const int&, const int&)"
No matching function for call to ‘std::less<int>::less(const int&, const int&)’
我试着写:
#include <functional>
template<class T, class func = std::less<T>>
class Car {
public:
void check(const T& x, const T& y) {
func(x, y); //.... << problem
}
};
int main() {
Car<int> car;
car.check(6, 6);
return 0;
}
我在这里的意思是,它会识别int
的通常<
,但它说的是我标记的地方:
调用"std::less::less(const int&,constint(amp;)'
但如果我用自定义func
创建一个Car
,那么它就可以工作。。。我该怎么解决?
您的问题是需要func
的实例,因为std::less<T>
是一个函子(即类类型),而不是函数类型。当你有
func(x, y);
实际上,您尝试使用x
和y
作为构造函数的参数来构造一个未命名的std::less<T>
。这就是为什么你得到
没有用于调用"std::less::less(const int&,const int&)"的匹配函数
因为CCD_ 10是构造函数调用。
你可以看到它是这样工作的:
#include <functional>
template<class T, class func = std::less<T>>
class Car {
func f;
public:
void check(const int& x, const int& y) {
f(x, y);
// or
func()(x, y); // thanks to Lightness Races in Orbit http://stackoverflow.com/users/560648/lightness-races-in-orbit
}
};
int main() {
Car<int> car;
car.check(6, 6);
return 0;
}
实时示例
相关文章:
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?
- 调用'myStud::myStud(int, const char [5], int, int, int)'没有匹配函数
- 返回对象时从'const DList<int>* const'到 'DList<int>*' [-fallowive] 的转换无效
- int(int)& 或 int(int) const &是什么类型?
- 传递 const int* 和 int* 时调用重载函数的不同版本(const int* const&/&&)
- 如何在构造函数中初始化int const * const
- 对"displayForStudent(int, int const*, double const*, int)"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
- 如何在 C++ 中解释"const int *const & variable"
- 为什么"int & const" MSVC 编译得很好?
- const int const&和const int&in C++有什么区别?
- std::set<int * const> 不会编译
- " invalid operands of types 'int' and 'int* const'" 来自功能的错误,来自书
- std::initializer_list<int const> 不能从 std::initializer_list <int>构造
- 对'std::string Helper::ToString<int>(int const&)'的未定义引用
- 无法弄清楚 int[3][3] 和 int(* const)[3] 之间的区别
- const int* const fun(const int* const& p) const的含义;
- 为什么vector<int * const>是非法/不合逻辑的?
- 对 Stack<int>::p ush(int const&) 的未定义引用
- JsonCpp:未定义的对"Json::Value::operator[](int) const"的引用