C++ “sqrt”不是“std”的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16518210/
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
'sqrt' is not a member of 'std'
提问by Yakov
I compile my program in linux - it has the following line :
我在 linux 中编译我的程序 - 它有以下几行:
std::sqrt((double)num);
On windows it is ok,but on linux I get 'sqrt' is not a member of 'std' I have an include for math.h
在 Windows 上没问题,但在 linux 上我得到 'sqrt' 不是 'std' 的成员我有一个包含 math.h
what is a problem with it?
它有什么问题?
回答by jrok
Change the directive to #include <cmath>
. C++ headers of the form <cxxxxxxx>
are guaranteed to have the standard names in std
namespace (and may optionaly provide them in global namespace). <xxxxxx.h>
are not.
将指令更改为#include <cmath>
. 形式的 C++ 标头<cxxxxxxx>
保证在std
命名空间中具有标准名称(并且可以选择在全局命名空间中提供它们)。<xxxxxx.h>
不是。
回答by Arne Mertz
it is simply because <math.h>
does not declare the functions in namespace std
. It has been included into the C++ standard only for compatibility reasons. The correct C++ include would be <cmath>
.
这仅仅是因为<math.h>
没有声明namespace std
. 仅出于兼容性原因,它才包含在 C++ 标准中。正确的 C++ 包含应该是<cmath>
.
§D.5,2
§D.5,2
Every C header, each of which has a name of the form
name.h
, behaves as if each name placed in the standard library namespace by the correspondingcname
header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope of the namespacestd
and are then injected into the global namespace scope by explicit using-declarations.
每个 C 头文件,每个头文件都有一个形式为 的名称,其
name.h
行为就好像由相应cname
头文件放置在标准库名称空间中的每个名称都放置在全局名称空间范围内。未指定这些名称是否首先在命名空间的命名空间范围内声明或定义std
,然后通过显式using-declarations注入到全局命名空间范围中。
That your code worked under windows was pure luck - if you want to call it so. The last sentence gives a hint what might happen under windows, but not under linux: under windows, obviously the names are valid in both the global namespace and namespace std
.
您的代码在 Windows 下运行纯属运气 - 如果您想这样称呼它。最后一句暗示了在 windows 下可能会发生什么,但在 linux 下不会发生:在 windows 下,显然这些名称在 global namespace 和 namespace 中都是有效的std
。