我试图运行此功能,但我一直收到错误。
#include <iostream>
#include <thread>
using namespace std;
void primeNumbers( int num, int prime )
{
cout << "The prime numbers equal to or below " << num << " are: " << endl;
for ( int i = 1; i <= num; i++ )
{
prime = 0;
for( int j = 2;j <= i/2; j++ )
{
if( i % j ==0 )
{
prime++;
break;
}
}
if ( prime == 0 && i != 1 )
cout << i << endl;
}
}
int main()
{
int num;
int prime = 0;
cout << "Enter a positive integer: ";
cin >> num;
std::thread primeN( primeNumbers );
primeN.join( );
system("pause");
return 0;
}
我已从输出窗口复制了所有内容。
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: With the following template arguments:
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Callable=void (__cdecl *)(int,int)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Types={}'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0>(std::tuple<void (__cdecl *)(int,int)> &,std::integer_sequence<_Ty,0>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>,
1> _Ty=size_t
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0>(std::tuple<void (__cdecl *)(int,int)> &,std::integer_sequence<_Ty,0>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>,
1> _Ty=size_t
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(256): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thread(52): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,int)>,std::default_delete<std::tuple<void (__cdecl *)(int,int)>>>
1> ]
1> c:\users\andre\documents\school\csci\prime\prime\testprime.cpp(34): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(int,int),,void>(_Fn)' being compiled
1> with
1> [
1> _Fn=void (__cdecl &)(int,int)
1> ]
我对多线程知之甚少,因为我昨天刚刚开始学习它。所以,如果我做了一些完全错误的事情,请告诉我。
答案 0 :(得分:1)
您必须为可调用对象提供参数
示例:
std::thread primeN( primeNumbers,42, 0 );
如果您希望更新prime
,请使用:
std::thread primeN( primeNumbers,42, std::ref(prime) );