简体   繁体   English

C ++错误C2893:无法专门化功能模板

[英]c++ error C2893: Failed to specialize function template

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. 因此,如果我做的事情完全错误,请告诉我。

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 : 如果您希望更新prime ,请使用:

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

See here 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2893:无法专门化功能模板C ++ - error C2893: Failed to specialize function template C++ 错误C2893:无法专门化功能模板 - error C2893: Failed to specialize function template 错误C2893:无法使用CTPL专门化功能模板 - error C2893: Failed to specialize function template with CTPL 错误:'无法专门化功能模板'C2893'std :: invoke' - Error: 'Failed to specialize function template' C2893 'std::invoke' 错误C2893:无法专门化函数模板VC ++ 2012 - Error C2893: Failed to specialize function template VC++ 2012 错误C2893无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)&#39; - Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…)' 错误C2893:无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)noexcept( <expr> )” - error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…) noexcept(<expr>)' 错误C2893:无法专门化功能模板&#39;unknown-type std :: less <void> :: operator()(_ Ty1 &amp;&amp;,_ Ty2 &amp;&amp;)const&#39; - error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const' C ++错误C2893 - C++ error C2893 Visual C ++ 2015 std :: function与C2893 - Visual C++ 2015 std::function vs C2893
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM