我对此感到非常沮丧。我阅读了大多数stackoverflow注释和示例,但都没有显示出任何作用。我刚开始做c ++。
我希望代码打印“ Hello World”。
这是我的代码示例(我可以提供完整的代码,但由于其中有很多代码,因此我将不再需要):
#include <iostream>
#include <thread>
using namespace std;
class Greeting {
public:
void hi() {
cout << "Hello World!\n";
}
};
int main() {
Greeting g;
thread t(g.hi);
t.join();
return 0;
}
错误:
||=== Build: Debug in Cookie Island (compiler: GNU GCC Compiler) ===| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional ||In instantiation of 'struct std::_Bind_check_arity<void (Greeting::*)()>':| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional |1538|required from 'struct std::_Bind_simple_helper<void (Greeting::*)()>'| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional |1552| required by substitution of 'template<class _Callable, class ... _Args> typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (Greeting::*)(); _Args = {}]'| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\thread |142|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Greeting::*)(); _Args = {}]'| C:\Users\Andrew Shen\Desktop\Cookie Island\main.cpp|51|required from here| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional |1426|error: static assertion failed: Wrong number of arguments for pointer-to-member| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional ||In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Greeting::*)()>()>':| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\thread |142|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Greeting::*)(); _Args = {}]'| C:\Users\Andrew Shen\Desktop\Cookie Island\main.cpp|51|required from here| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional |1505|error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Greeting::*)()>()>'| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional |1526|error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Greeting::*)()>()>'| ||=== Build failed: 4 error(s), 7 warning(s) (0 minute(s), 1 second(s)) ===|
更新: 这更像是我的代码。
#include <iostream>
#include <thread>
#include <string>
using namespace std;
class Greeting {
public:
void hi(string txt) {
cout << "Hello World!"+txt << endl;
}
};
int main() {
string t = " More...";
Greeting g;
thread t(g.hi, &g, t);
t.join();
return 0;
}
Blockquote || ===构建:在Cookie Island中进行调试(编译器:GNU GCC编译器)=== | C:\ Program Files(x86)\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 5.1.0 \ include \ c ++ \ functional ||实例化为'struct std :: _ Bind_simple)>(Greeting *,std :: __ cxx11: :basic_string *)>':| C:\ Program Files(x86)\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 5.1.0 \ include \ c ++ \ thread | 142 |从'std :: thread :: thread(_Callable &&,_Args && ...)[与_Callable = void(Greeting :: )(std :: __ cxx11 :: basic_string); _Args = {问候,std :: __ cxx11 :: basic_string,std :: allocator> *}]'| C:\ Users \ Andrew Shen \ Desktop \ Cookie Island \ main.cpp | 52 |从此处要求| C:\ Program Files(x86)\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 5.1.0 \ include \ c ++ \ functional | 1505 |错误:'class std :: result_of)中没有名为'type'的类型> *,std :: __ cxx11 :: basic_string *)>'| C:\ Program Files(x86)\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 5.1.0 \ include \ c ++ \ functional | 1526 |错误:'class std :: result_of)中没有名为'type'的类型> *,std :: __ cxx11 :: basic_string *)>'| || ===构建失败:2个错误,3个警告(0分钟,1秒)=== |
答案 0 :(得分:0)
成员函数不是对象的一部分,而是类。您需要完整的限定功能名称。您需要执行以下操作:
thread t(&Greeting::hi, &g, str);
此外,您不小心将't'用作凝视变量的名称以及该线程的名称,我将其修改为字符串的str。
答案 1 :(得分:0)
以下任何作品,
string str = " More...";
thread t(Greeting::hi, g, str);
或
thread t(&Greeting::hi, g, str);
或
thread t(Greeting::hi, &g, str);
或
thread t(&Greeting::hi, &g, str);
能否请您解释所有这些情况之间的区别。 @Abhinav