簡體   English   中英

奇怪的是“無法推斷'T'的模板參數”錯誤

[英]Strange “Could not deduce template argument for 'T'” error

錯誤在代碼中:

//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>(...)

采用

row = ConditionalInput<int>(...) 

明確指定返回類型。

我注意到如果必須將其顯式調用為Conditional<int>(...) ,則還需要先指定返回類型。

template <class T, class A>
T function (A) { ... }

而以下將產生編譯錯誤:

template <class A, class T>
T function (A) { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM