目标获取指针args的std :: thread错误

时间:2015-02-02 00:04:23

标签: c++ c++14 stdthread

我的设置类似于以下内容,

A.hpp

#include <thread>

class A {

    static void foo(char*, char*);

    void bar() {

        char* char_start = (char*) malloc(100 * sizeof(char));
        char* char_end = char_start + (100 * sizeof(char)) - 1;

        std::thread t(foo, char_start, char_end);
        t.join();

        return;
    }
};

的main.cpp

int main() { 
    A.bar();            

    return 0;
}

我用

编译
g++ -std=c++14 -pthread main.cpp

得到:

usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(long unsigned int*, char*, char*, const bool&); _Args = {long unsigned int*, char*, char*}]’
A.cpp:100:151:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                         ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’
 _M_invoke(_Index_tuple<_Indices...>)

我无法解释这个错误。非常感谢任何帮助。

编辑:我道歉,因为我遗漏了foo的默认bool参数设置为false的事实。摆脱默认参数让我们编译。

1 个答案:

答案 0 :(得分:0)

A.bar()不是有效的语言构造。您只能在bar()的实例上调用A,而不能在类型名称A上调用。{/ p>

通过更改

A.bar();          

A a;
a.bar();

并提供了A::foo()的虚拟实现,我能够构建并运行该程序。