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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:32:48  来源:igfitidea点击:

'round' is not a member of 'std'

c++g++

提问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 roundfunction many times in ISO C++98before. Unusually, roundand ::roundboth work.

我知道我以前roundISO C++98 中多次使用过这个函数。与众不同的是,round::round这两个工作。

What gives?

是什么赋予了?

Update: I was compiling with g++ -std=c++98 -Wall -pedantic. Switching to std=c++0xworks.

更新:我正在编译g++ -std=c++98 -Wall -pedantic。切换到std=c++0x工作。

But why do the unqualified/anonymous roundand ::roundboth work if std::rounddoesn't?

但是为什么不合格/匿名round::round两者都可以工作,如果std::round没有呢?

采纳答案by juanchopanza

The std::roundfunctions 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:

我做了一些研究,这是我发现的:

roundis defined in ISO C++11, as it contains the ISO C99standard library.

roundISO C++11 中定义,因为它包含ISO C99标准库。

roundis 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 stdfor 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 ::roundworks.

这就是为什么::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 roundfunction (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 roundfunction. In C++11, it is ISO/IEC 9899:1999 (C99), which doesinclude the roundfunction.

在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.hfrom 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 roundfunction is not available in C++03, so compile with the -std=c++0xoption and refer to it as std::round.

因此,总结一下,该round函数在 C++03 中不可用,因此使用该-std=c++0x选项进行编译并将其称为std::round.