使用boost :: thread将const指针参数传递给函数

时间:2013-01-19 11:07:19

标签: c++ multithreading pointers boost parameters

我有如下功能

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

当我尝试使用boost :: thread

运行它时
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’

确实,我是新手来提升。 谢谢。

1 个答案:

答案 0 :(得分: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功能