简体   繁体   English

C ++中具有模板返回类型的模板函数

[英]Template function with template return type in C++

Relevant portion of the .h file: .h文件的相关部分:

template<class T, class W>
T inputValidate( T input, W minVal, W maxVal);

Relevant portion of the .cpp file: .cpp文件的相关部分:

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" 我收到“错误:'T'未命名类型”的错误

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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