C++ 对重载函数的不明确调用 - std::to_string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14617950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ambiguous call to overloaded function - std::to_string
提问by ShenDraeg
In attempting to insert integer values into a string, I thought that my prayers were answered when I found std::to_string, but for some reason whenever I actually try to use it, Visual Studio complains about ambiguity. Here is the current incarnation of my function:
在尝试将整数值插入字符串时,我认为当我找到 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;
}
I have tried casting temp_int inside the call to to_string, and as you can see I even tried casting the result of what should be integer division, but no matter what I do, VS spits this out at me:
我尝试在 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)'
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Rapptz
MSVC11 lacks the proper overloads for std::to_string
so you have to do a static_cast
to unsigned long long
or long long
MSVC11缺少适当的重载std::to_string
,所以你必须做static_cast
对unsigned long long
或long long
Note that this bug is fixed in the November CTP 2012. Which you can get here.
请注意,此错误已在 2012 年 11 月的 CTP 中修复。您可以在此处获取。
回答by Naps62
temp_int
is a int
value, and Visual Studio seems to detect only overloads which receive either double
, long long
or unsigned long long
values, so it doesn't know which overload to use, thus the ambiguity (although it would seem intuitive to cast integer to long values)
temp_int
是一个int
值,而 Visual Studio 似乎只检测接收double
,long long
或unsigned long long
值的重载,因此它不知道要使用哪个重载,因此存在歧义(尽管将整数转换为长值似乎很直观)
Either declare temp_int
as a long long
, or cast it when invoking the function
要么声明temp_int
为 a long long
,要么在调用函数时将其强制转换