传递参数,以提高::线程没有重载函数采用2个参数 [英] Passing parameter to boost::thread no overloaded function takes 2 arguments

查看:289
本文介绍了传递参数,以提高::线程没有重载函数采用2个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从升压::线程文件似乎我可以这样传递参数给线程函数:

From the boost::thread documentation it seems that I can pass parameters to the thread function by doing this:

boost::thread* myThread = new boost::thread(callbackFunc, param);

然而,当我这样做,编译器抱怨

However, when I do this the compiler complains that

没有重载函数需要两个参数

no overloaded function takes 2 arguments

我的code:

#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,我想可能是很古老的增强传来的副本。有趣的是,虽然,我看了看thread.hpp,它确实有2个或多个参数的构造函数模板。

I am using the copy of boost that came with Ogre3d, which I suppose could be very old. Interestingly, though, I took a look at thread.hpp and it does have the templates for constructors with 2 or more parameters.

推荐答案

的问题是,成员函数需要一个隐含的第一个参数键入* ,其中键入是类的类型。这是其中的成员函数被调用的类型的实例机制,意味着你必须一个额外的参数传递给的boost ::线程构造函数。您还可以通过成员函数作为&安培的地址;类名:: functionName

The problem is that member functions take an implicit first parameter Type*, where Type is the type of the class. This is the mechanism by which member functions get called on instances of types, and means you have to pass an extra parameter to the boost::thread constructor. You also have to pass the address of the member function as &ClassName::functionName.

我已经做了一个小的编译和运行的例子,我希望说明了如何使用:

I have made a small compiling and running example that I hope illustrates the use:

#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();
}

这篇关于传递参数,以提高::线程没有重载函数采用2个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆