执行类的成员函数 [英] Executing a member function of a class

查看:227
本文介绍了执行类的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以一种方式实验C ++ 11线程,它接受一个类的成员函数作为参数到线程构造函数,如下面的第20行标记的第一个代码片段所示。类定义在第二个代码段中给出。当这个代码编译时,我得到一堆错误显示在第三个片段。任何人都可以告诉我我做错了什么?感谢。



SNIPPET 1:主题初始化(main_app.cpp)

  #include< thread> 
#includeServiceRegistrar.hpp

#define SERVER_TYPE 100065
#define SERVER_INST_LOWER 1
#define SERVER_INST_UPPER 2
#define TIMEOUT 500000

int main()
{
ServiceRegistrar sr1(SERVER_TYPE,TIMEOUT,SERVER_INST_LOWER,SERVER_INST_LOWER);
/ * LINE 20是以下* /
std :: thread t(& ServiceRegistrar :: subscribe2TopologyServer,sr1);
t.join();
sr1.publishForSRs();

}

SNIPPET 2:类别定义

  class ServiceRegistrar 
{
public:
ServiceRegistrar(int serverType,int serverTimeOut,int serverInstanceLower,int serverInstanceUpper)
:mServerType(serverType),
mServerTimeOut(serverTimeOut),
mServerInstanceLower(serverInstanceLower),
mServerInstanceUpper(serverInstanceUpper)
{}

void subscribe2TopologyServer();
void publishForSRs();
void publishForServices();

private:
int mServerType;
int mServerTimeOut;
int mServerInstanceLower;
int mServerInstanceUpper;
};

SNIPPET 3:编译输出

  $ g ++ -g -c -Wall -std = c ++ 11 main_app.cpp -pthread 
在/ usr / include / c ++ / 4.7 / ratio:38:0,
来自/usr/include/c++/4.7/chrono:38,
来自/usr/include/c++/4.7/thread:38,
来自main_app .cpp:8:
/usr/include/c++/4.7/type_traits:在实例化'struct std :: _ Result_of_impl< false,false,std :: _ Mem_fn< void(ServiceRegistrar :: *)()> ,ServiceRegistrar>':
/usr/include/c++/4.7/type_traits:1857:12:required from'class std :: result_of< std :: _ Mem_fn< void(ServiceRegistrar :: *)()> ServiceRegistrar)>'
/usr/include/c++/4.7/functional:1563:61:需要从'struct std :: _ Bind_simple< std :: _ Mem_fn< void(ServiceRegistrar :: *)()> ServiceRegistrar)>'
/usr/include/c++/4.7/thread:133:9:需要从'std :: thread :: thread(_Callable&&&&& ...)[with _Callable = void(ServiceRegistrar :: *)(); _Args = {ServiceRegistrar&}]'
main_app.cpp:20:64:从这里需要
/usr/include/c++/4.7/type_traits:1834:9:错误: (std :: _ Mem_fn< void(ServiceRegistrar :: *)()>)(ServiceRegistrar)'


解决方案

显然它是一个 gcc 4.7 bug ... use

  std :: thread t(& ServiceRegistrar :: subscribe2TopologyServer, & sr1); 


你可能不想将 sr1 复制到 t 的thread-local-storage,所以这更好


I am trying to experiment the C++11 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown in the first code snippet below on line 20 that is marked . The class definition is given in the 2nd code snippet. When this code is compiled, I am getting a bunch of errors shown in the 3rd snippet. Can anyone tell me what I am doing wrong? Thanks.

SNIPPET 1: Thread initialization (main_app.cpp)

#include <thread>
#include "ServiceRegistrar.hpp"

#define SERVER_TYPE  100065
#define SERVER_INST_LOWER  1
#define SERVER_INST_UPPER  2
#define TIMEOUT  500000

int main()
{
  ServiceRegistrar sr1(SERVER_TYPE, TIMEOUT, SERVER_INST_LOWER, SERVER_INST_LOWER);
      /*LINE 20 is the following*/
  std::thread t(&ServiceRegistrar::subscribe2TopologyServer, sr1);
t.join();
  sr1.publishForSRs();

}

SNIPPET 2: Class definition

class ServiceRegistrar
{
  public:
    ServiceRegistrar(int serverType, int serverTimeOut, int serverInstanceLower, int serverInstanceUpper)
       : mServerType(serverType),
         mServerTimeOut(serverTimeOut),
         mServerInstanceLower(serverInstanceLower),
         mServerInstanceUpper(serverInstanceUpper)
         { }

     void subscribe2TopologyServer();
     void publishForSRs();
     void publishForServices();

  private:
     int mServerType;
     int mServerTimeOut;
     int mServerInstanceLower;
         int mServerInstanceUpper;           
  };

SNIPPET 3: Compilation output

  $ g++ -g -c -Wall -std=c++11 main_app.cpp -pthread
  In file included from /usr/include/c++/4.7/ratio:38:0,
             from /usr/include/c++/4.7/chrono:38,
             from /usr/include/c++/4.7/thread:38,
             from main_app.cpp:8:
  /usr/include/c++/4.7/type_traits: In instantiation of ‘struct    std::_Result_of_impl<false, false, std::_Mem_fn<void (ServiceRegistrar::*)()>,    ServiceRegistrar>’:
  /usr/include/c++/4.7/type_traits:1857:12:   required from ‘class std::result_of<std::_Mem_fn<void (ServiceRegistrar::*)()>(ServiceRegistrar)>’
  /usr/include/c++/4.7/functional:1563:61:   required from ‘struct std::_Bind_simple<std::_Mem_fn<void (ServiceRegistrar::*)()>(ServiceRegistrar)>’
 /usr/include/c++/4.7/thread:133:9:   required from ‘std::thread::thread(_Callable&&,  _Args&& ...) [with _Callable = void (ServiceRegistrar::*)(); _Args = {ServiceRegistrar&}]’
 main_app.cpp:20:64:   required from here
/usr/include/c++/4.7/type_traits:1834:9: error: no match for call to     ‘ (std::_Mem_fn<void (ServiceRegistrar::*)()>) (ServiceRegistrar)’

解决方案

Apparently it's a gcc 4.7 bug...use

std::thread t(&ServiceRegistrar::subscribe2TopologyServer, &sr1);

instead.

EDIT: actually, you probably don't want to be copying sr1 to the thread-local-storage of t, so this is better anyway.

这篇关于执行类的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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