【问题标题】:c++ error C2893: Failed to specialize function templatec++错误C2893:无法专门化函数模板
【发布时间】:2015-09-21 04:49:16
【问题描述】:

我正在尝试运行此程序,但一直收到错误消息。

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

我对多线程了解不多,因为我昨天才开始学习它。所以如果我做错了什么,请告诉我。

【问题讨论】:

  • void primeNumbers( int num, int prime ) 需要两个参数,而您没有提供它们。

标签: c++ multithreading templates compiler-errors


【解决方案1】:

您必须为可调用对象提供参数

例子:

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

如果您希望更新您的prime,请使用:

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

See here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多