Template function with template return type in C++
Relevant portion of the .h file:
template<class T, class W>
T inputValidate( T input, W minVal, W maxVal);
Relevant portion of the .cpp file:
T inputVa开发者_StackOverflow社区lidate( T input, W minVal, W maxVal)
{
if (input < minVal || input > maxVal)
{
cout << "Invalid input! Try again: ";
cin input;
}
return input;
}
I get an error of "error: ‘T’ does not name a type"
You need to repeat the template declaration before your function definition:
template<class T, class W>
T inputValidate( T input, W minVal, W maxVal)
{
...
}
You must define the function as:
template <class T, class W> T inputValidate(T input, W minVal, W maxVal) {
}
精彩评论