6

The error is in this code:

//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; });

The error is:

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

I've been struggling with it for hours but can't seem to find a solution. I believe mistake might be trivial, but I couldn't find anyone else encountering the error under similar circumstances. Help much appreciated!

EDIT: Correction made by Frederik Slijkerman fixes one issue but creates another. This time the error is:

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

Please bear with me and help me solve this issue.

0

3 Answers 3

6

C++ cannot deduce the return type of a function. It only works with its arguments. You have to explicitly call ConditionalInput<int>(...).

2
  • Edited, since <> needed to be replaced by lt / gt.
    – Scharron
    Commented Jul 24, 2010 at 10:14
  • Correct answer in the comments of the answer above. Although this user holds the credits for it.
    – Johnny
    Commented Jul 24, 2010 at 10:20
3

Use

row = ConditionalInput<int>(...) 

to specify the return type explicitly.

7
  • I do not have to specify second template type?
    – Johnny
    Commented Jul 24, 2010 at 10:08
  • After that I've got another error, this one giving me even greater headache. it's like this: 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:\Users\CodeMaster\documents\visual studio 2010\Projects\Challenge8 - Pascals Triangle\Challenge8 - Pascals Triangle\c_main.obj Challenge8 - Pascals Triangle
    – Johnny
    Commented Jul 24, 2010 at 10:11
  • did you put your templated function ConditionalInput in a .cpp file ?
    – Scharron
    Commented Jul 24, 2010 at 10:13
  • Yes I did. And made a proper prototype in the proper header file.
    – Johnny
    Commented Jul 24, 2010 at 10:14
  • 1
    Templated functions need to be put in header files, or if you want to have definition away from declaration, in a .hxx file included by the .h file.
    – Scharron
    Commented Jul 24, 2010 at 10:17
0

I have noticed you also need to specify the return type first if it has to be called explicitly as Conditional<int>(...).

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

whereas the following will produce compile errors:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.