gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:52 24 4
gpt4 key购买 nike

错误在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com