1

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
}
Max
  • 1,295
  • 6
  • 16
  • 31
  • 1
    This compiler error shows that gcc thinks that the `ratio` is a function type. Is that _exactly_ how you've defined `ratio`? – CB Bailey Jul 22 '10 at 21:57
  • It's reporting `T1` as std::basic_string, std::allocator >, which is to say, `std::string`. (Stupid thing doesn't want to parse my backticks!) – jdmichal Jul 22 '10 at 22:01
  • @jdmichal: T1 is governed by the return type but look at the parameters in line 2 of the error message before the list of candidates. "unresolved overloaded function type" and const TCODColor&. – CB Bailey Jul 22 '10 at 22:11
  • @GMan: +1 for helping me fix my problem, even if it was indirectly. :) – Max Jul 22 '10 at 22:16
  • @Max There must be something else in scope at the time which is also called `ratio` (e.g. a function or a global variable) which is not of type `string` and so was causing the error but hiding the true source. There's no way that g++ would simply fail to report the use of an undefined variable. – Tyler McHenry Jul 22 '10 at 22:29

3 Answers3

4

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.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
1

Here's how I would fix your code:

if (div > 1.0f)
{
    string str = verbose ? intToStr(hp) + "/" + intToStr(maxHP()) : "astring";
    return make_pair(str, someOtherType); 
}

A little more concise. Also, your formatting could be made a bit more generic.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
0

It looks like it might be a const issue. Look at the overloads it's reporting in the error:

  • pair(std::pair<_T1, _T2>&&)
  • pair(const _T1&, const _T2&)

So it looks like it's expecting const parameters.

jdmichal
  • 10,984
  • 4
  • 43
  • 42
  • If `ratio` is indeed a `string` (or more generically a value of type `_T1`), the second overload will work just fine. – GManNickG Jul 22 '10 at 22:00