C++ “round”不是“std”的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12696764/
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
'round' is not a member of 'std'
提问by QuasarDonkey
I'm try to compile this code:
我正在尝试编译此代码:
#include <cmath>
double gravity (double level) {
return 0.02 * std::round(level);
}
But GCC is telling me:
但海湾合作委员会告诉我:
error: 'round' is not a member of 'std'
I know I've used the round
function many times in ISO C++98before. Unusually, round
and ::round
both work.
我知道我以前round
在ISO C++98 中多次使用过这个函数。与众不同的是,round
与::round
这两个工作。
What gives?
是什么赋予了?
Update: I was compiling with g++ -std=c++98 -Wall -pedantic
.
Switching to std=c++0x
works.
更新:我正在编译g++ -std=c++98 -Wall -pedantic
。切换到std=c++0x
工作。
But why do the unqualified/anonymous round
and ::round
both work if std::round
doesn't?
但是为什么不合格/匿名round
和::round
两者都可以工作,如果std::round
没有呢?
采纳答案by juanchopanza
The std::round
functions are C++11, so you would need to compile with C++11 or a more recent standard enabled.
这些std::round
函数是 C++11,因此您需要使用 C++11 或启用了更新的标准进行编译。
回答by QuasarDonkey
I've done a bit of research, here's what I've found:
我做了一些研究,这是我发现的:
round
is defined in ISO C++11, as it contains the ISO C99standard library.
round
在ISO C++11 中定义,因为它包含ISO C99标准库。
round
is not part of the ISO C++98, which uses the ISO C90standard library.
round
不是ISO C++98 的一部分,它使用ISO C90标准库。
That's why it's not in namespace std
for C++98.
这就是为什么它不在C++98 的命名空间std
中。
But g++
is (incorrectly) including the C99headers, even when compiled with -std=c++98 -pedantic
, which should disable all non-standard stuff:
但是g++
(错误地)包括C99头文件,即使使用 编译-std=c++98 -pedantic
,也应该禁用所有非标准内容:
GNU_SOURCE is defined by G++ and ... it implies _USE_ISOC99
GNU_SOURCE 是由 G++ 定义的,......它意味着 _USE_ISOC99
(from http://gcc.gnu.org/ml/gcc/2002-09/msg00580.html)
(来自http://gcc.gnu.org/ml/gcc/2002-09/msg00580.html)
This is why ::round
works.
这就是为什么::round
有效。
This apparently is a bug in GCC: Why does GCC allow use of round() in C++ even with the ansi and pedantic flags?
这显然是 GCC 中的一个错误:为什么 GCC 允许在 C++ 中使用 round() 即使带有 ansi 和 pedantic 标志?
Other C++ compilers may not provide a round
function (since it's not required by the standard), so I should define my own.
其他 C++ 编译器可能不提供round
函数(因为标准不需要它),所以我应该定义我自己的。
回答by Mae
how about
怎么样
float myRoundFunc(float toRound)
{
return std::ceil(toRound - 0.5);
}
That way there is no comparison and it will round properly.
这样就没有比较,它会正确地舍入。
回答by MarianoGNU
old topic but maybe usefull for the ones searching in google:
旧主题,但可能对在 google 中搜索的人有用:
//TODO: C++11 got round() function defined in math.h
float distance = sqrt((float)(plus_x * plus_x + plus_y * plus_y));
if (distance >= floor(distance)+0.5) distance = ceil(distance);
else distance = floor(distance);
回答by Joseph Mansfield
In the C++03 standard, the normative reference for the standard C libraryis ISO/IEC 9899:1990 (C90), which does notinclude the round
function. In C++11, it is ISO/IEC 9899:1999 (C99), which doesinclude the round
function.
在C++03标准中,标准C库的规范参考是ISO/IEC 9899:1990 (C90),不包括round
函数。在C ++ 11,它是ISO / IEC 9899:1999(C99),其不包括round
功能。
The contents of <cmath>
in each C++ standard are the same as math.h
from the corresponding standard C library except for a few changes (§26.8/4 in C++11):
的内容<cmath>
中的每个C ++标准是一样的math.h
,从相应的标准C库除了少数改变(在C ++ 11§26.8/ 4):
The contents of these headers [
<cmath>
and<cstdlib>
] are the same as the Standard C library headers<math.h>
and<stdlib.h>
respectively [...]
这些头文件 [
<cmath>
和<cstdlib>
]的内容与标准 C 库头文件<math.h>
和<stdlib.h>
[...]
They are, however, in namespace scope of std
(§17.6.1.2/4 in C++11):
但是,它们在std
(C++11 中的第 17.6.1.2/4 节)的命名空间范围内:
In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std.
但是,在 C++ 标准库中,声明(在 C 中定义为宏的名称除外)在命名空间 std 的命名空间范围 (3.3.6) 内。
So, to wrap it up, the round
function is not available in C++03, so compile with the -std=c++0x
option and refer to it as std::round
.
因此,总结一下,该round
函数在 C++03 中不可用,因此使用该-std=c++0x
选项进行编译并将其称为std::round
.