1

I'm trying to run this but I keep receiving an error.

#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; } 

I've copied everything from the output window.

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> ] 

I don't know much about multithreading as I just started learning about it yesterday. So if I did something completely wrong please let me know.

1
  • 1
    void primeNumbers( int num, int prime ) is expecting two parameters, and you're not providing them. Commented Sep 21, 2015 at 4:52

1 Answer 1

1

You'll have to provide parameters to the callable object

Example :

std::thread primeN( primeNumbers,42, 0 ); 

And in case you want your prime to be updated use :

std::thread primeN( primeNumbers,42, std::ref(prime) ); 

See here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.