boost :: _ bi :: unwrapper <f> :: unwrap不能用作函数吗?</f>

时间:2013-05-07 06:57:14

标签: c++ boost-thread

尝试使用我在Windows中创建的线程类,在Linux中。选择Netbeans,在项目设置中我将Linker > Additional Library Directories指定为/usr/local/boost_1_53_0
Libraries下,我指定了boost_thread-mt
C++ Compiler > Include Directories下,我指定了/usr/local/boost_1_53_0

thread.hThread.cpp是我的档案。 Thread.hpp属于Boost。

显示的错误位于我已包含#include <boost/thread/thread.hpp>的行,错误为:

/usr/local/boost_1_53_0/boost/bind/bind.hpp: In member function ‘void  
boost::_bi::list1<A1>::operator()(boost::_bi::type<void>, F&, A&, int)
[with F = unsigned int (**)(void*), A = boost::_bi::list0, A1 =  
boost::_bi::value<void*>]’: In file included from  
/usr/local/boost_1_53_0/boost/bind.hpp:22:0,  
                 from /usr/local/boost_1_53_0/boost/thread/detail/thread.hpp:29,  
                 from /usr/local/boost_1_53_0/boost/thread/thread.hpp:22,  
                 from ../../../TSDK-master/src/Core/source/thread.h:72,  
                 from ../../../TSDK-master/src/Core/source/Thread.cpp:95:  
/usr/local/boost_1_53_0/boost/bind/bind_template.hpp:20:59:    
instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type  
boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = unsigned  
int (**)(void*), L = boost::_bi::list1<boost::_bi::value<void*> >,  
boost::_bi::bind_t<R, F, L>::result_type = void]’  
/usr/local/boost_1_53_0/boost/thread/detail/thread.hpp:117:17:    
instantiated from ‘void boost::detail::thread_data<F>::run() [with F =  
boost::_bi::bind_t<void, unsigned int (**)(void*),  
boost::_bi::list1<boost::_bi::value<void*> > >]’  
../../../TSDK-master/src/Core/source/Thread.cpp:202:50:   instantiated  
from here /usr/local/boost_1_53_0/boost/bind/bind.hpp:253:9: error:  
‘boost::_bi::unwrapper<F>::unwrap [with F = unsigned int  
(**)(void*)]((* & f), 0l)’ cannot be used as a function  

对于造成这种情况的原因,我完全不知道。有人可以帮忙吗?

修改
thread.h的一部分

#include <boost/thread/thread.hpp> 
#include <cstdio>

class Thread
{
protected:
    boost::thread*            m_Thread;
    unsigned int id;
private:
    bool                      m_pause;
    boost::mutex              m_pause_mutex;
    boost::condition_variable m_pause_changed;
    bool threadCreated;

public:

    Thread();
    Thread(unsigned int id);
    ~Thread();
    bool Start();
    bool CreateThread(unsigned int Function(void* pParam),void *pParam);
    void Terminate();
    unsigned int myThreadProc(void* param);
    bool IsRunning();
    bool IsStarted();

protected:
    virtual unsigned int ThreadProc() = 0;
};

和thread.cpp的一部分

#include "thread.h"
Thread::Thread() : m_Thread(NULL), m_pause(true), threadCreated(false), id(NULL)
{
}//Thread ctor

Thread::Thread(unsigned int _id) : m_Thread(NULL), id(_id), threadCreated(false), m_pause(true)
{}

Thread::~Thread()
{
    if (NULL != m_Thread) { delete m_Thread; }
}

unsigned int Thread::GetThreadID() { return id; }//GetThreadID

bool Thread::Start()
{
    m_Thread = new boost::thread(&Thread::myThreadProc, this, this);
    threadCreated = true;
    return threadCreated;
}

bool Thread::CreateThread(unsigned int Function(void* pFunctionParam),void *pParam)
{
    //Receives Thread Function,pParam as Arguments and starts Thread.
    m_Thread= new boost::thread(&Function,pParam);
    threadCreated = true;
    return threadCreated;
}

void Thread::Terminate()
{
    if (NULL != m_Thread) {
        m_Thread->join();
        m_Thread=0;
    }
    else {printf("No thread was created\n");}
}

unsigned int Thread::myThreadProc(void* param)
{
    Thread* ourBoostThread = (Thread*) param;
    int ret = ourBoostThread->ThreadProc();
    return ret;
}//_ThreadProc

bool Thread::IsRunning() { return !m_pause; }
bool Thread::IsStarted() { return threadCreated; }

第202行是bool Thread::IsStarted() { return threadCreated; }

1 个答案:

答案 0 :(得分:2)

m_Thread= new boost::thread(&Function,pParam);

Function实际上是一个指向函数的指针,即使它看起来不像。如果声明函数类型的函数参数,那么它实际上是一个函数指针;就像声明为数组的函数参数实际上是指针一样。

所以&Function是指向该函数指针的指针,不能像函数一样调用它。将其更改为Function,可以是。{/ p>