문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top