2013-01-19 80 views
0

我有一個函數如下傳遞常量指針參數與升壓::線程功能

void test(const int * x, int d){ 
    for(int i=0; i<d ; i++) 
     cout<< x[i] << endl; 
} 

當我嘗試用一​​個boost ::線程

int n=10; 
int * x1=new int[n]; 
boost::thread *new_thread = new boost::thread(& test,x1,n); 

我得到運行以下編譯錯誤

error: no matching function for call to ‘boost::thread::thread(<unresolved overloaded function type>, int*&, uint16_t&)’ 
/usr/include/boost/thread/detail/thread.hpp:216: note: candidates are: boost::thread::thread(boost::detail::thread_move_t<boost::thread>) 
/usr/include/boost/thread/detail/thread.hpp:155: note:     boost::thread::thread() 
/usr/include/boost/thread/detail/thread.hpp:123: note:     boost::thread::thread(boost::detail::thread_data_ptr) 
/usr/include/boost/thread/detail/thread.hpp:114: note:     boost::thread::thread(boost::thread&) 
main.cpp:397: warning: unused variable ‘new_thread’ 

確實,我是新手來提高。 謝謝。

回答

3

看起來你test函數重載:

//here 
void test(const int * x, int d){ 
    for(int i=0; i<d ; i++) 
     cout<< x[i] << endl; 
} 

//somewhere 
void test() 
{ 
    std::cout <<"hahahahaha\n"; 
} 

現在,當你指定test

boost::thread *new_thread = new boost::thread(& test,x1,n); 

編譯器無法知道,如果你想使用一個test功能,或其他。

  • 應指定什麼超載要使用:

    boost::thread *new_thread = new boost::thread((void(*)(const int*, int)) test,x1,n); 
    
  • 或重命名test功能