以下代码导致cl.exe崩溃(MS VS2005).
我试图使用boost bind来创建一个调用myclass方法的函数:
#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
class myclass {
public:
void fun1() { printf("fun1()\n"); }
void fun2(int i) { printf("fun2(%d)\n", i); }
void testit() {
boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) );
boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails
f1();
f2(111);
}
};
int main(int argc, char* argv[]) {
myclass mc;
mc.testit();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想在某个类中按某种顺序对某个结构的向量进行排序.我在类中编写了struct和predicate函数的定义,并在具有这些struct和function的类的方法中运行std :: sort.但是发生了编译错误.gcc版本是4.0.1,OS是Mac OSX.代码如下:
class sample {
public:
struct s {
int x;
int y;
};
bool cmp (struct s a, struct s b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
int func(void) {
std::vector <struct s> vec;
// ...
sort(vec.begin(), vec.end(), cmp); // compilation error
// ...
return 0;
}
};
int main(void) {
sample *smp = new sample();
smp->func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息庞大而复杂.所以这是前两行.
sortSample.cpp:在成员函数'INT ::样品FUNC()':
sortSample.cpp:51:错误:类型的参数'布尔(样品::)(样品:: S,样品:: S)'不匹配'bool(sample ::*)(sample :: …