找回密码
 欢迎注册
楼主: wayne

[求助] no matching function for call to bind(unresolved overloaded function type

[复制链接]
 楼主| 发表于 2010-8-5 13:35:58 | 显示全部楼层
functional实际上一个类对象,竟然可以转化为函数指针吗?难道是实现里面提供了转为指针的操作符? mathe 发表于 2010-8-5 12:00
functors可以是: * class object with operator() * function pointer * member function pointer
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-8-5 13:45:36 | 显示全部楼层
关于 重载函数的bind问题,boost官网给了两种解决办法,呵呵,前面都提到过。 http://www.boost.org/doc/libs/1_ ... html#err_overloaded
Binding an overloaded function An attempt to bind an overloaded function usually results in an error, as there is no way to tell which overload was meant to be bound. This is a common problem with member functions with two overloads, const and non-const, as in this simplified example: struct X { int& get(); int const& get() const; }; int main() { boost::bind( &X::get, _1 ); } The ambiguity can be resolved manually by casting the (member) function pointer to the desired type: int main() { boost::bind( static_cast< int const& (X::*) () const >( &X::get ), _1 ); } Another, arguably more readable, alternative is to introduce a temporary variable: int main() { int const& (X::*get) () const = &X::get; boost::bind( get, _1 ); }
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-8-5 14:37:08 | 显示全部楼层
21# wayne 写了一个 Demo code:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <tr1/functional>
  5. using std::tr1::placeholders::_1;
  6. //normal function
  7. void foo(int i) { std::cout<<"Foo\t"<<i<<std::endl; }
  8. //class object with operator()
  9. class Bar{
  10. public:
  11. void operator () (int i) { std::cout<<"Bar\t"<< i<<std::endl; }
  12. };
  13. //member function
  14. class Ctest {
  15. public:
  16. void puts(std::string &s) {std::cout<<"STRING:\t"<<s<<std::endl;}
  17. void puts(double x) {std::cout <<"DOUBLE:\t"<<x<<std::endl;}
  18. };
  19. int main(){
  20. Bar bar;Ctest fm;
  21. std::tr1::function<void (int)> f(&foo); f(2009);
  22. std::tr1::function<void (int)> b=bar; b(2010);
  23. double a[7] = { .8, .7, .9, .5, .3};
  24. std::vector<double> v(a,a+5);
  25. sort(v.begin(),v.end());
  26. void (Ctest::*fn)(double) = &Ctest::puts;
  27. std::for_each(v.begin(), v.end(), std::tr1::bind(fn, fm, _1));
  28. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-11-21 21:19 , Processed in 0.027612 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表