我正在尝试熟悉 boost 线程和信号。因此,我实现了这个非常简单的代码,它由一个实现线程的类(Class1)组成。我希望这个类能够通过信号接收来提供服务。为此,我刚刚开始利用信号增强库,但出现此错误:
/home/andrea/libs/boost_1_50_0/boost/bind/mem_fn.hpp:359:22: error: invalid use of non-static member function
当我尝试在 Eclipse 环境中使用 gcc 编译它时。单例有什么问题还是绑定到实例方法?
这是 Class1.cpp
#include "Class1.hpp"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include "Package1.hpp"
Class1::Class1(){
boost::thread thread(boost::bind(&Class1::classifierBehavior,this));
};
void Class1::classifierBehavior(){
service.run();
Package1Signals::getInstance()->signal1.connect(boost::bind(&Class1::method1, boost::ref(*this)));
};
void Class1::method1(Signal1 signal1){}
和 Package1.hpp
#ifndef PACKAGE1_HEADER
#define PACKAGE1_HEADER
#include <boost/signal.hpp>
struct Signal1{
int foo;
};
class Package1Signals{
private:
Package1Signals();
static Package1Signals * instance;
public:
boost::signal<void (Signal1)> signal1;
static Package1Signals * getInstance(){
if(!instance){
instance = new Package1Signals();
}
return instance;
};
};
#endif