user450632
user450632

Reputation: 1

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"

Upvotes: 0

Views: 1723

Answers (2)

Anand
Anand

Reputation: 729

You must define the function as:

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

}

Upvotes: 1

casablanca
casablanca

Reputation: 70691

You need to repeat the template declaration before your function definition:

template<class T, class W>
T inputValidate( T input, W minVal, W maxVal)
{
  ...
}

Upvotes: 3

Related Questions