c++ - 使用 tr1::function 时出现编译错误

标签 c++ compilation functor tr1

目的是在CDevVS890中调用m_callback_fn时执行CVS890Executor::do_full_frame。

以下是相关代码:

"CDevVS890.h"
typedef std::tr1::function<void (void* frame, int len)> DoFrameFn; 

class CDevVS890
{
public:
    CDevVS890();

    void receive();   

    DoFrameFn m_callback_fn;
}

"CDevVS890.cpp"
void CDevVS890::receive()
{
    ...
    m_callback_fn((void*)frame, (int)len);
}

/*----------------------------------------------------------------------*/

"CVS890Executor.h"
class CVS890Executor
{
public:
    CVS890Executor();

private:
    void hookup_to_DevVS890();
    void do_full_frame( void* frame, int len );
}

"CVS890Executor.cpp"
CVS890Executor::CVS890Executor()
{
    hookup_to_DevVS890();
}

void CVS890Executor::hookup_to_DevVS890()
{
m_pDevVS890 = new CDevVS890();
m_pDevVS890->m_callback_fn = 
    std::tr1::bind(&CVS890Executor::do_full_frame, this, _1);
}

void CVS890Executor::do_full_frame(void* frame, int len)
{
   ...
} 

错误很多且很难阅读:

在/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1/functional:56 包含的文件中, 来自 ../../src/Common/CDevVS890.h:17, 来自 CVS890Executor.h:13, 来自 CVS890Executor.cpp:8: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:在成员函数âtypename std::tr1中: :result_of<_Functor(typename std::tr1::result_of 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1: :_Bind<_Functor(_Bound_args ...)>::__call(const std::tr1::tuple<_UElements ...>&, std::tr1::_Index_tuple<_Indexes ...>) [with _Args = void *&, int&, int ..._Indexes = 0, 1, _Functor = std::tr1::_Mem_fn, _Bound_args = CVS890Executor*, std::tr1::_Placeholder<1>]â: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1191: 从âtypename std::tr1 实例化::result_of<_Functor(typename std::tr1::result_of 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::operator()(_Args& ...) [with _Args = void*, int, _Functor = std::tr1::_Mem_fn, _Bound_args = CVS890Executor*, std::tr1::_Placeholder<1>]â /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1668:从 âstatic void std::实例化tr1::_Function_handler::_M_invoke(const std::tr1::_Any_data&, _ArgTypes ...) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _ArgTypes = void*, int]â /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:2005:从âstd::tr1实例化: :function<_Res(_ArgTypes ...)>::function(_Functor, typename __gnu_cxx::__enable_if<(!std::tr1::is_integral::value), std::tr1::function<_Res(_ArgTypes .. .)>::Useless>::_type) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _Res = void, _ArgTypes = void*, int]â /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1885: 从âtypename __gnu_cxx::__enable_if 实例化<(! std::tr1::is_integral::value), std::tr1::function<_Res(ArgTypes ...)>&>::_type std::tr1::function <_Res(_ArgTypes ...)>::operator=(_Functor) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _Res = void, _ArgTypes = void*, int]â CVS890Executor.cpp:115: 从这里实例化 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: 错误:调用不匹配â(std::tr1::_Mem_fn) (CVS890Executor*&, void*&)â /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:546: 注意:候选人是:_Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void, _Class = CVS890Executor, _ArgTypes = void*,整数] /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:551: 注意:_Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void, _Class = CVS890Executor, _ArgTypes = void*, int] /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: 错误:返回语句带有值,在返回“void”的函数中 make: * [CVS890Executor.o] 错误 1

知道这有什么问题吗?

干杯

最佳答案

你忘记了第二个参数。您对 bind 函数的调用应该是这样的:

std::tr1::bind(&CVS890Executor::do_full_frame, this, _1, _2);
//                                                       ^^

关于c++ - 使用 tr1::function 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316723/

相关文章:

C++ 在循环中捕获异常并在循环结束后重新抛出?

c++ - OpenCV RGB值检测?

c++ - 错误 : class "" has no member named ""? 是否可以解决此特定情况?

c++ - 不同编译器的不同输出 - C 和 C++

C++ 数组大小初始化

java - 使用 JAR 内的 list 编译 JAR?

ios - 使用 Cocoapods 复制符号 iOS

module - .ml 文件中定义的模块如何引用自身

generics - 具有任何参数列表的函数的通用仿函数

c++ - 如何在 C++ 中创建自适应仿函数?