简体   繁体   中英

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 inputValidate( 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) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM