【问题标题】:compiler error on calling boost::bind() inside boost::thread constructor在 boost::thread 构造函数中调用 boost::bind() 时出现编译器错误
【发布时间】:2013-08-18 10:36:41
【问题描述】:

我目前正在编写一个 firebreath C++ NPAPI 插件,并且我正在尝试从插件内部调用 boost::thread。我正在构建的平台是 Ubuntu Linux 13.04。下面是类声明的骨架和相关的成员函数实现:

class EmulatorLaunchPluginAPI : public FB::JSAPIAuto
{
public:
   EmulatorLaunchPluginAPI(const EmulatorLaunchPluginPtr& plugin, 
                          const FB::BrowserHostPtr& host):m_plugin(plugin), m_host(host)
  {
      registerMethod("launch_emulator", 
                   make_method(this, &EmulatorLaunchPluginAPI::launch_emulator));
       registerMethod("launch_emulator_thread", 
                   make_method(this, &EmulatorLaunchPluginAPI::launch_emulator_thread));
  }
    virtual ~EmulatorLaunchPluginAPI() {};
    EmulatorLaunchPluginPtr getPlugin()
    {
        EmulatorLaunchPluginPtr plugin(m_plugin.lock());
        if (!plugin) {
            throw FB::script_error("The plugin is invalid");
        }
        return plugin;
    }  

    bool launch_emulator(const std::string& ,const FB::JSObjectPtr& )
    {
         emt(boost::bind(//boost::type<void>(),
        &EmulatorLaunchPluginAPI::launch_emulator_thread,
        this,
        cmd,
                callback));
        return true;
    }
    void launch_emulator_thread(const std::string& , const FB::JSObjectPtr& )
    {
         //thread body logic here
         int result = 0;
         result = invoke_command(cmd); 
         //callback to the browser
         callback->InvokeAsync("", FB::variant_list_of(shared_from_this())(result));
    }

private:
    int invoke_command(const std::string& )
    {
        int res = system("/usr/bin/firefox"); 
        return res;
    }

    EmulatorLaunchPluginWeakPtr m_plugin;
    FB::BrowserHostPtr m_host;
    boost::thread emt;  
};

I am getting the following compile error for the code fragmented highlighted above:

[ 54%] 构建 CXX 对象项目/EmulatorLaunchPlugin/CMakeFiles/EmulatorLaunchPlugin.dir/EmulatorLaunchPluginAPI.cpp.o /home/ajay/Downloads/firebreath-FireBreath-c335f5b/projects/EmulatorLaunchPlugin/EmulatorLaunchPluginAPI.cpp:在成员函数“bool EmulatorLaunchPluginAPI::launch_emulator(const string&, const JSObjectPtr&)”中: /home/ajay/Downloads/firebreath-FireBreath-c335f5b/projects/EmulatorLaunchPlugin/EmulatorLaunchPluginAPI.cpp:94:30: 错误:不匹配调用'(boost::thread) (boost::_bi::bind_t&, const boost ::shared_ptr&>, boost::_bi::list3, boost::_bi::value >, boost::_bi::value > > >)' make[2]: * [projects/EmulatorLaunchPlugin/CMakeFiles/EmulatorLaunchPlugin.dir/EmulatorLaunchPluginAPI.cpp.o] 错误 1 make[1]: [projects/EmulatorLaunchPlugin/CMakeFiles/EmulatorLaunchPlugin.dir/all] 错误 2 make: ** [all] 错误 2

我是 Boost 库的新手,我确实尝试了解 boost::bind 的工作原理,但我无法解决此错误。有人可以帮我理解编译器的行为吗?

问候, 阿杰

【问题讨论】:

  • 请使用 C++ cmets 来指示错误站点,因为 '***emt(...' 看起来像 emt 的三次取消引用。在下一个 ; *** return 之后,我的内部解析器得到了 ICE %)
  • 还有错误信息。看起来像是部分复制粘贴...call to ‘(boost::thread) (boost::_bi::bind_t&amp;, const boost::shared_ptr&amp;&gt; -- 我没有看到相应的开放尖括号
  • @zaufi :感谢您的批评...我已将编译器输出包含在块引号中,这导致了文本的误传...请在帖子的修订版中找到原始错误...问候,阿杰

标签: c++ boost-thread firebreath boost-bind member-function-pointers


【解决方案1】:

尝试将行更改为:

emt = boost::thread(&EmulatorLaunchPluginAPI::launch_emulator_thread, this, cmd, callback));

【讨论】:

    【解决方案2】:

    我仍然没有清楚地看到错误消息(特别是我对没有模板类型的shared_ptr 和在它之后悬空的&gt; 感到困惑),但我还是在您的代码中看到了一些错误:

    1. launch_emulator 使用此代码无法观察到的 smth。例如名为cmdcallback 的smth。使用我的心灵感应,我猜 错过了函数参数名称(我说的对吗?)

    2. 该类包含 未初始化 boost::thread 实例 -- 您必须在 ctor 中对其进行初始化,因为该类没有复制 ctor 或分配运算符 (对于 C++11 模式,它有 move constructor/assign

    3. launch_emulator 中,您调用operator() 以获得boost::thread 实例。这个班级没有这样的成员......所以我想你实际上关于这个事实的简短错误消息......

    【讨论】:

    • 感谢您对复制构造函数的评论...这解决了我的问题!!我同意这是非常基本的:-)...我将 emt 从一个类字段更改为使其成为 launch_emulator() 的局部变量。代码现在已经编译好了!
    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2017-02-20
    • 1970-01-01
    • 2018-06-10
    相关资源
    最近更新 更多