Question

I'm a beginner in C+=, just wrote my Hello World today.

#include <iostream>
int main(){
double x = 6.25;
x = sqrt(x);
std::cout << x;
return 0;
}

This worked in Visual Studio, no error message, while adding:

#include <cmath> 

worked fine as well.

But with a GCC compiler online the previous code returns with

main.cpp: In function 'int main()':
main.cpp:5:12: error: 'sqrt' was
not declared in this scope x = sqrt(x);
                                     ^

Please help, thanks.

Était-ce utile?

La solution

There is no auto include behaviour.. by including <iostream> you are indirectly including <cmath>.

It's just the way that Microsoft implemented the C++ standard library, they wanted to use some <cmath> functions in so they needed to include it in the header file.

I recommend you read this article.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top