c++ - 奇怪的 "Could not deduce template argument for ' T'"错误

标签 c++ templates functional-programming lambda

错误在this代码:

//myutil.h
template <class T, class predicate>
T ConditionalInput(LPSTR inputMessage, LPSTR errorMessage, predicate condition);    

//myutil.cpp
template <class T, class Pred>
T ConditionalInput(LPSTR inputMessage, LPSTR errorMessage, Pred condition)
{
        T input
        cout<< inputMessage;
        cin>> input;
        while(!condition(input))
        {
                cout<< errorMessage;
                cin>> input;
        }
        return input;
}

...

//c_main.cpp 
int row;

row = ConditionalInput("Input the row of the number to lookup, row > 0: ",
"[INPUT ERROR]: Specified number is not contained in the range [row > 0]. "
"Please type again: ", [](int x){ return x > 0; });

错误是:

Error   1       error C2783: 'T ConditionalInput(LPSTR,LPSTR,predicate)' :
could not deduce template argument for 'T' c_main.cpp        17      1

我已经为此苦苦挣扎了几个小时,但似乎找不到解决方案。我相信错误可能是微不足道的,但我找不到其他人在类似情况下遇到错误。 非常感谢帮助!

编辑:Frederik Slijkerman 所做的更正解决了一个问题,但造成了另一个问题。这次的错误是:

Error   1   error LNK2019: unresolved external symbol "int __cdecl ConditionalInput<int,class `anonymous namespace'::<lambda0> >(char *,char *,class `anonymous namespace'::<lambda0>)" (??$ConditionalInput@HV<lambda0>@?A0x109237b6@@@@YAHPAD0V<lambda0>@?A0x109237b6@@@Z) referenced in function _main

请耐心等待并帮助我解决这个问题。

最佳答案

C++ 无法推断函数的返回类型。它只适用于它的参数。 您必须显式调用 ConditionalInput<int>(...) .

关于c++ - 奇怪的 "Could not deduce template argument for ' T'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324640/

相关文章:

c++ - 使用 mex : No supported compiler or SDK was found 时出错

templates - Helm 图表嵌套循环

javascript - 在 Node.js 中的函数中访问 __dirname 是否被视为不纯代码?

scala - Scala 有守卫吗?

c++ - 单元测试 native C++ 代码

c++ - cl::Image3D 在 nVidia TITAN black 上出现段错误,但在 Intel openCL 设备上没有?

c++ - 如何让 glutTimerFunct 调用子类函数

c++ - 模板默认为具有更多模板的模板

visual-studio-2010 - 如何使用我创建的 Visual Studio 2010 项目模板

haskell - Haskell : Syntax error in input (unexpected `=' )