我的设置类似于以下内容,
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
的事实。摆脱默认参数让我们编译。
答案 0 :(得分:0)
A.bar()
不是有效的语言构造。您只能在bar()
的实例上调用A
,而不能在类型名称A
上调用。{/ p>
通过更改
A.bar();
到
A a;
a.bar();
并提供了A::foo()
的虚拟实现,我能够构建并运行该程序。