C++ 将整数转换为std::pair接受的std::字符串

C++ 将整数转换为std::pair接受的std::字符串,c++,stdstring,std-pair,C++,Stdstring,Std Pair,我有一个将整数转换为std::string的函数: std::string intToStr(const int n) { stringstream ss; ss << n; return ss.str(); } 使用g++编译时失败,出现以下错误: src/Stats.cpp:231: error: no matching function for call to ‘std::pair<std::basic_string<char, std

我有一个将整数转换为std::string的函数:

std::string intToStr(const int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}
使用g++编译时失败,出现以下错误:

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>&)
有人知道我怎么解决这个问题吗


修正了它,尽管答案很奇怪。我的问题是,不知何故,比率并没有得到定义,但g++并没有告诉我。按照GMan的建议,将我的代码改为使用
make\u pair
,这让它突然告诉了我。有人知道为什么会这样吗

以下是该功能的更多内容:

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
}

看起来这可能是一个
const
问题。查看它在错误中报告的重载:

  • pair(std::pair&&
  • pair(常数T1&,常数T2&)

所以它看起来需要
const
参数。

它看起来可能是一个
const
问题。查看它在错误中报告的重载:

  • pair(std::pair&&
  • pair(常数T1&,常数T2&)

因此,它看起来需要
const
参数。

失败的原因:

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

变量的作用域仅限于在其中定义的块

失败的原因:

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

变量的作用域仅限于在其中定义的块

下面是我如何修复您的代码:

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

更简洁一点。此外,还可以设置您的格式。

以下是我如何修复您的代码:

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


更简洁一点。此外,还可以进行格式化。

此编译器错误表明gcc认为
比率是一种函数类型。这正是您定义比率的方式吗?它将
T1
报告为std::basic_string,也就是说,
std::string
。(愚蠢的东西不想解析我的回执!)@jdmichal:T1由返回类型控制,但请查看候选列表之前错误消息第2行中的参数。“未解决的重载函数类型”和const TCODColor&.@GMan:+1,以帮助我解决问题,即使它是间接的。:)@Max当时范围内必须存在另一种类型,也被称为
比率
(例如函数或全局变量),该类型不是
字符串
,因此导致了错误,但隐藏了真正的源。g++不可能不报告未定义变量的使用情况。此编译器错误表明gcc认为
比率
是一种函数类型。这正是您定义比率的方式吗?它将
T1
报告为std::basic_string,也就是说,
std::string
。(愚蠢的东西不想解析我的回执!)@jdmichal:T1由返回类型控制,但请查看候选列表之前错误消息第2行中的参数。“未解决的重载函数类型”和const TCODColor&.@GMan:+1,以帮助我解决问题,即使它是间接的。:)@Max当时范围内必须存在另一种类型,也被称为
比率
(例如函数或全局变量),该类型不是
字符串
,因此导致了错误,但隐藏了真正的源。g++不可能不报告未定义变量的使用。如果
比率
确实是
字符串
(或者更一般地说是
\u T1
类型的值),第二个重载将正常工作。如果
比率
确实是
字符串
(或者更一般地说是
\u T1
)类型的值,第二次过载可以正常工作。啊!我真丢脸。这就是我掉牙套的原因。谢谢,啊!我真丢脸。这就是我掉牙套的原因。谢谢,好主意。谢谢不过,你知道你的链接指向了这个页面吗?@Max:对不起,它指向了一个被人删除的答案。我把它转到别的地方去了。好主意。谢谢不过,你知道你的链接指向了这个页面吗?@Max:对不起,它指向了一个被人删除的答案。我已经把它转到别的地方去了。
if (div > 1.0f)
{
    string str = verbose ? intToStr(hp) + "/" + intToStr(maxHP()) : "astring";
    return make_pair(str, someOtherType); 
}