简体   繁体   English

C ++数学函数问题(在Linux下)

[英]C++ math functions problem (under Linux)

I'm having problem regarding max and sqrt 我在最大和平方数方面遇到问题

If I include math.h it coudn't find sqrt. 如果我包含math.h,则找不到sqrt。

So I view the cmath header file and inside it includes math.h, but when I try to open math.h it says that file is not found. 因此,我查看了cmath头文件,并且其中包含math.h,但是当我尝试打开math.h时,该文件未找到。 SO ithink my math.h is missing in Linux. 所以我想我的math.h在Linux中不见了。

Sorry I found the answer. 对不起,我找到了答案。

I just need to write it this way: 我只需要这样写:

std::max std::sqrt std :: max std :: sqrt

But Why does it work without "std::" under Windows OS? 但是,为什么在Windows操作系统中不带“ std ::”就可以工作?

NB: in C++ you should #include <cmath> not #include <math.h> 注意:在C ++中,您应该#include <cmath>而不是#include <math.h>

NB: also specifying the namespace is a good practice 注意:还指定名称空间是一个好习惯

It's possible that the reason that you did not need to use std:: previously, is because somewhere in a headerfile the following statement was written: 您以前不需要使用std ::的原因可能是因为在头文件中的某处写入了以下语句:

using namespace std;

After this statement, the 'std::' prefix is not necessary anymore. 在此语句之后,不再需要'std ::'前缀。

Hope this clarified things... 希望这可以澄清事情...

Your system likely has the C headers in one place in the file system, and the C++ headers in another. 您的系统可能在文件系统的一个位置有C头,而在另一位置有C ++头。 Are you familiar with the actual list of directories searched for system headers? 您是否熟悉搜索系统标题的目录的实际列表? (Actually, the implementation is not required to have system header files, although all the ones I'm familiar with do. The C++ standard has requirements on what the statement #include <cmath> has to do, but not on how it has to be done.) (实际上,尽管我熟悉的所有实现都不需要实现具有系统头文件。C++标准对语句#include <cmath>必须执行的操作有要求,但对必须执行的操作没有要求。完成。)

In your answer, you talk about variations between C++ on Linux and Windows. 在回答中,您将讨论Linux和Windows上C ++之间的差异。 These are not OS-specific, but rather implementation-specific. 这些不是特定于操作系统的,而是特定于实现的。 You're probably using Visual C++ on Windows and something else on Linux (if only because VC++ runs only on Windows). 您可能在Windows上使用Visual C ++,而在Linux上使用其他东西(如果仅是因为VC ++仅在Windows上运行)。 They may work differently in default configurations. 它们在默认配置下可能会有所不同。

In fact, #include <math.h> should be like #include <cmath> , except that math.h should move all its function names and such into the std:: namespace. 实际上, #include <math.h>应该类似于#include <cmath> ,只是math.h应该将其所有函数名称等移入std ::名称空间。 If this isn't happening in your Linux C++ system, there's a problem. 如果您的Linux C ++系统中没有发生这种情况,那就有问题。 Unfortunately, you haven't provided nearly enough information to figure out what's happening. 不幸的是,您没有提供足够的信息来了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM