简体   繁体   中英

C++ math functions problem (under Linux)

I'm having problem regarding max and sqrt

If I include math.h it coudn't find 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. SO ithink my math.h is missing in Linux.

Sorry I found the answer.

I just need to write it this way:

std::max std::sqrt

But Why does it work without "std::" under Windows OS?

NB: in C++ you should #include <cmath> not #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:

using namespace std;

After this statement, the 'std::' prefix is not necessary anymore.

Hope this clarified things...

Your system likely has the C headers in one place in the file system, and the C++ headers in another. 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.)

In your answer, you talk about variations between C++ on Linux and Windows. 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). 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. If this isn't happening in your Linux C++ system, there's a problem. Unfortunately, you haven't provided nearly enough information to figure out what's happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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