Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:47:25+00:00 2026-06-03T20:47:25+00:00

I am trying to write a simple function that will get me a number

  • 0

I am trying to write a simple function that will get me a number from user input within a range.

When instanciating this function i explicitly tell it i want it instanciated with int but still i get the error:

thermo.cpp:105:31: error: no matching function for call to ‘getInput(int&)’

Why is is trying to find a function that takes int& as argument?

template<class T, T min = std::numeric_limits<T>::min, T max = std::numeric_limits<T>::max>
T getInput(T default_value = T()){
  std::string input;
  T myNumber = T(); //default inits
  while(true){
    getline(cin, input);

    if(input.length() == 0){
      return default_value;
    }

    // This code converts from string to number safely.
    stringstream myStream(input);
    if (myStream >> myNumber){
      if(myNumber > max || myNumber < min){
        stringstream ss;
        ss << "Input out of bounds. Received " << myNumber << " expected between " << min << " and " << max << ".";
        throw invalid_argument(ss.str());
      }
      return myNumber;
    }

    cout << "Invalid number, please try again" << endl;
  }
}

void get(const std::string& prompt, int& param){
  cout << prompt << " [" << param << "]:";
  param = getInput<int,0>(param); // i specifically tell it i want 'int', why am i getting 'int&'?
}

Update

If i try CharlesB suggestion:

void get(const std::string& prompt, int& param){
  cout << prompt << " [" << param << "]:";
  param = getInput<int,0>(int(param));
}

i get

thermo.cpp:105:36: error: no matching function for call to ‘getInput(int)’

Forgot:

g++ 4.5.3 under cygwin

Command line:

$ g++ thermo.cpp -o thermo.exe -Wall -pedantic -std=c++0x

Update 2

if i call it like this

void get(const std::string& prompt, int& param){
  cout << prompt << " [" << param << "]:";
  param = getInput<int,0,15>(int(param)); // fully parameterized
}

it works… but i’d rather not specify an upper bound (not even numeric_limits) on each call.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:47:26+00:00Added an answer on June 3, 2026 at 8:47 pm

    Don’t use templates for min and max:

    template<class T>
    T getInput(T default_value = T(), T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max());
    

    There is no reason to use templates for those arguments(besides the fact that it does not work).

    Edit: You can’t use those arguments as template values since std::numeric_limits<T>::min() is a function, its value is known on runtime, and template value arguments have to be bound to a value at compile time. This is valid:

    template<class T, T min = 0, T max = 5>
    T getInput(T default_value);
    

    Since 0 and 5 are known during compilation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple javascript function that will clone some html input
I'm trying to write a simple php function that will strip everything between two
I'm trying to write a simple Vim function that takes the name of a
I trying to write a simple function to call unmanaged code from managed code.
I'm trying to write a simple google extension that will upon clicking ctrl+alt+x search
I'm trying to write functions that will allow me to marshal/unmarshal simple structs into
I'm trying to write a simple Ubiquity command that will run a query on
I am trying to write, what I thought was, a simple jQuery function to
I'm trying to write a simple program in VC++ which will just initialize the
I am trying to write a powershell script that will take in a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.