请大牛们帮忙看看这段代码,在编译的时候会有如下error message 请问如何能编译通
过啊?跪谢!
当然这只是个虚构的例子,主要是想在template存在的情况下,传递一个member
function的指针给另一个member function.
test1.cpp: In member function 'void Foo::testInt()':
test1.cpp:30:61: error: no matching function for call to 'Foo::processVector
(std::vector&, std::_Bind_helperstd::_Placeholder<1>&>::type)'
processVector(arr, std::bind(&Foo::print, this, _1));
^
test1.cpp:21:8: note: candidate: template void Foo::processVector(
const std::vector&, std::function)
void processVector(const vector& vec, function fun) {
^
test1.cpp:21:8: note: template argument deduction/substitution failed:
test1.cpp:30:61: note: 'std::_Bind(Foo*,
std::_Placeholder<1>)>' is not derived from 'std::function'
processVector(arr, std::bind(&Foo::print, this, _1));
------------------------------------
class Foo {
public:
template
void print(T val) {
cout << val << endl;
}
template
void processVector(const vector& vec, function fun) {
for (auto it=vec.begin(); it != vec.end(); ++it) {
fun(*it);
}
}
void testInt() {
int samples[] = {1,2,3};
vector arr(samples, samples+3);
processVector(arr, std::bind(&Foo::print, this, _1));
}
void testDouble() {
double samples[] = {0.1, 0.2, 0.3};
vector arr(samples, samples+3);
processVector(arr, std::bind(&Foo::print, this, _1));
}
};