c++ - 对重载函数的模糊调用 - std::to_string

标签 c++ string visual-studio c++11 overloading

在尝试将整数值插入字符串时,我认为当我找到 std::to_string 时我的祈祷得到了回应,但出于某种原因,每当我实际尝试使用它时,Visual Studio 都会提示模棱两可。这是我的功能的当前化身:

string get_time_remaining (int elapsed)
{
    string remaining;
    string temp_string;

    int time_remaining = TimeLimit - elapsed;

    int temp_int;

    temp_int = int(time_remaining / 3600);

    if(temp_int == 0)
        remaining = "00 : ";
    else
    {
        temp_string = std::to_string(temp_int);   // Here!
        remaining = temp_string + " : ";
    }

    temp_int = time_remaining % 60 + 1;

    if(temp_int < 10)
        remaining = remaining + "0";

    temp_string = std::to_string(temp_int);

    remaining = remaining + temp_string;

    return remaining;
}

我尝试在 to_string 的调用中强制转换 temp_int,如您所见,我什至尝试强制转换应该是整数除法的结果,但无论我做什么,VS 都会向我吐出这个:

d:\my programs\powerplay\powerplay\powerplay.cpp(1285): error C2668: 'std::to_string' : ambiguous call to overloaded function
1>          d:\microsoft visual studio 10.0\vc\include\string(688): could be 'std::string std::to_string(long double)'
1>          d:\microsoft visual studio 10.0\vc\include\string(680): or       'std::string std::to_string(_ULonglong)'
1>          d:\microsoft visual studio 10.0\vc\include\string(672): or       'std::string std::to_string(_Longlong)'

任何帮助将不胜感激。

最佳答案

MSVC11 缺少 std::to_string 的适当重载,因此您必须对 unsigned long longlong 执行 static_cast

请注意,此错误已在 2012 年 11 月 CTP 中修复。您可以获得 here .

关于c++ - 对重载函数的模糊调用 - std::to_string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617950/

相关文章:

javascript - 从字符串列表中删除前缀

c - 为什么在这个程序中 strcpy() 函数会产生不需要的输出?

c# - 在 C# 中使用 VB Net 制作的 VB 库

c#:发送文件,混合成功文件和损坏文件

c++ - 距离路由 vector 实现

Python C 扩展 : Extract parameter from the engine

c++ - 我们应该在何时、何地以及为什么使用 "BigObject&& rv = std::move(big_obj);"?

c++ - Eclipse CDT Kepler 不允许 "Display as array..."

regex - 用 perl 查找和替换双换行符?

c# - System.Net.Http.HttpRequestException : Device not configured ---> System.Net.Sockets.SocketException:设备未配置