![](/img/trans.png)
[英]'std::thread::thread': no overloaded function takes 7 arguments
[英]Passing parameter to boost::thread no overloaded function takes 2 arguments
从boost :: thread文档看来,我可以通过执行以下操作将参数传递给线程函数:
boost::thread* myThread = new boost::thread(callbackFunc, param);
但是,当我这样做时,编译器会抱怨
没有重载函数需要2个参数
我的代码:
#include <boost/thread/thread.hpp>
void Game::playSound(sf::Sound* s) {
boost::thread soundThread(playSoundTask, s);
soundThread.join();
}
void Game::playSoundTask(sf::Sound* s) {
// do things
}
我使用的是Ogre3d随附的boost副本,我认为它可能很旧。 不过,有趣的是,我看了一下thread.hpp,它确实具有带有2个或更多参数的构造函数的模板。
问题在于成员函数采用隐式的第一个参数Type*
,其中Type
是类的类型。 这是在类型的实例上调用成员函数的机制,这意味着您必须将一个额外的参数传递给boost::thread
构造函数。 您还必须将成员函数的地址传递为&ClassName::functionName
。
我做了一个小的编译和运行示例,希望可以说明该用法:
#include <boost/thread.hpp>
#include <iostream>
struct Foo
{
void foo(int i)
{
std::cout << "foo(" << i << ")\n";
}
void bar()
{
int i = 42;
boost::thread t(&Foo::foo, this, i);
t.join();
}
};
int main()
{
Foo f;
f.bar();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.