我无法使用boost :: signals删除编译错误。任何想法都将不胜感激。
由于我已经移植了一个2年前编写的程序,适应当前环境,我仍然是boost :: signals的新手。以下代码是我修改原始程序以简化问题目的。
我想直接解决我的问题。但除此之外,因为有很多关于提升::信号的问题(但我已经放弃弄清楚哪一个是对我的情况最合适/最接近),我会对答案中建议的那些进行投票。为了使这个问题成为一个更好的存档问题,请提出更多正确的问题标题。
boostProve_Doc.h
#ifndef FC_H
#define FC_H
#include <vector>
#include "iostream"
#include "boost/signal.hpp"
typedef boost::signal<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PSlot;
class Doc {
public:
Doc(uint width, uint height) {
std::cout << "In constructor. width= " << width << ", height= "
<< height << std::endl;
}
~Doc() {
//Do ...
}
void connectPostUpdate(PSlot s) {
sig.connect(s);
}
protected:
PostUpdateSignal sig;
};
#endif
boostProve_View.cpp:
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/bind.hpp>
#include <boostProve_Doc.h>
using namespace std;
class View {
public:
View() {
setupCrowd();
}
~View() {
//do something...
}
private:
Doc *crowd_;
void setupCrowd() {
crowd_->connectPostUpdate(&View::correctR);
}
void correctR() {
// do something..
}
};
int main(int argc, char** argv) {
View c();
return 0;
}
错误:
$ g++ boostProve_View.cpp -I . /usr/lib/libboost_signals.so
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
from /usr/include/boost/function.hpp:64,
from /usr/include/boost/thread/future.hpp:20,
from /usr/include/boost/thread.hpp:24,
from boostProve_View.cpp:3:
/usr/include/boost/function/function_template.hpp: In member function ‘void boost::function0<R>::assign_to(Functor) [with Functor = void (View::*)(), R = void]’:
/usr/include/boost/function/function_template.hpp:722: instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:1064: instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
boostProve_View.cpp:20: instantiated from here
/usr/include/boost/function/function_template.hpp:903: error: no class template named ‘apply’ in ‘struct boost::detail::function::get_invoker0<boost::detail::function::member_ptr_tag>’
/usr/include/boost/function/function_template.hpp:913: error: too many initializers for ‘boost::detail::function::basic_vtable0<void>’
/usr/include/boost/function/function_template.hpp: In member function ‘bool boost::detail::function::basic_vtable0<R>::assign_to(F, boost::detail::function::function_buffer&) [with F = void (View::*)(), R = void]’:
/usr/include/boost/function/function_template.hpp:915: instantiated from ‘void boost::function0<R>::assign_to(Functor) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:722: instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:1064: instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
boostProve_View.cpp:20: instantiated from here
/usr/include/boost/function/function_template.hpp:492: error: no matching function for call to ‘boost::detail::function::basic_vtable0<void>::assign_to(void (View::*&)(), boost::detail::function::function_buffer&, boost::detail::function::basic_vtable0<R>::assign_to(F, boost::detail::function::function_buffer&) [with F = void (View::*)(), R = void]::tag)’
环境:Ubuntu 10.10,g ++(Ubuntu / Linaro 4.4.4-14ubuntu5)4.4.5
9/30/11更新)感谢您的建议,我通过这样做解决了这个问题:
crowd_->connectPostUpdate(boost::bind(&View::correctR, this));
答案 0 :(得分:1)
问题是你的插槽需要一个void (*)()
类型的函数,而你给它一个类型为void (View::*)()
的参数。遗憾的是,成员函数指针与函数指针不同。解决此问题的一种方法是使correctR
为静态,这将为其提供正确的签名。如果失败,您可能想要调查诸如Boost的Bind库之类的东西。
有关成员函数指针的更多信息,请参阅this part of the C++ FAQ Lite。