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 946395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:54:29+00:00 2026-05-15T22:54:29+00:00

I have this function that converts an integer to a std::string: std::string intToStr(const int

  • 0

I have this function that converts an integer to a std::string:

std::string intToStr(const int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}

It’s worked well so far, but now I’m trying to construct a string to put into a std::pair, and I’m having some trouble.

Given an integer variable hp and a function that returns an integer int maxHP(), I want to construct a string that looks like this: "5/10" (if hp is 5 and maxHP returns 10).

here’s my attempt:

string ratio = intToStr(hp) + "/" + intToStr(maxHP());
return pair<string, OtherType>(ratio, someOtherType); 

Compiling with g++, it fails with this error:

src/Stats.cpp:231: error: no matching function for call to  
‘std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
TCODColor>::pair(<unresolved overloaded function type>, const TCODColor&)’
/usr/include/c++/4.4/bits/stl_pair.h:92: note: candidates are: std::pair<_T1,
_T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, _T2 = TCODColor]
/usr/include/c++/4.4/bits/stl_pair.h:83: note:                 std::pair<_T1,
_T2>::pair(const _T1&, const _T2&) [with _T1 = std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, _T2 = TCODColor]
/usr/include/c++/4.4/bits/stl_pair.h:79: note:                 std::pair<_T1, 
_T2>::pair() [with _T1 = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, _T2 = TCODColor]
/usr/include/c++/4.4/bits/stl_pair.h:68: note: 
std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
TCODColor>::pair(const std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, TCODColor>&)

so std::pair doesn’t like my string. I’ve confirmed that it’s not OtherType causing the problem, because I have another pair constructor that compiles fine:

pair<string, OtherType>("astring", someOtherType);

Anyone see how I can fix this?


Fixed it, though the answer was odd. My problem was that somehow ratio wasn’t getting defined, but g++ didn’t tell me about it. Changing my code to use make_pair as GMan suggested suddenly made it tell me. Anyone know why that would happen?

Here’s more of the function:

if(verbose) 
    string ratio = intToStr(hp) + "/" + intToStr(maxHP());

if(div > 1.0f) {
    if(verbose) return pair<string, OtherType>(ratio, someOtherType); // doesn't compile
    else return pair<string, OtherType("astring", someOtherType); // compiles
}

here’s the fixed code:

string ratio = intToStr(hp) + "/" + intToStr(maxHP());

if(div > 1.0f) {
    if(verbose) return make_pair(ratio, someOtherType); // compiles now
    else return make_pair("astring", someOtherType); // compiles
}
  • 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-05-15T22:54:30+00:00Added an answer on May 15, 2026 at 10:54 pm

    The reason that this fails:

    if(verbose) 
        string ratio = intToStr(hp) + "/" + intToStr(maxHP());
    
    // the block of the if is now over, so any variables defined in it
    // are no longer in scope
    

    is that a variable’s scope is limited to the block it is defined in.

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

Sidebar

Related Questions

I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function called ProperCase that takes a string, then converts the first
I have this function in my program that converts integers to strings: QString Stats_Manager::convertInt(int
I have this function that I would like to condense into some iterator. How
I have this function that prints the name of all the files in a
I have this function that takes the input of a, runs a calculation and
I have this function that switches the HTML contents from one element on a
So I have this function that forks N number of child processes. However it
I have this jquery function that works, except I need to add something. I
I have this Javascript function that sends username and password to php file through

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.