Reputation: 212
Why does the following code works perfectly on a g++ compiler, but gives error on Visual Studio IDE?
#include<iostream>
using namespace std;
main()
{
cout <<"this is a test"<<
return 0;
}
In visual studio, I have to change code to the following:
#include<iostream>
using namespace std;
int main()
{
cout <<"this is a test"<<
return 0;
}
Upvotes: 0
Views: 429
Reputation: 119877
The first program is invalid C++, but since this style of function definition (with no explicit return type) was once common in C, g++ lets you get away with it by default for sake of backwards compatibility. Visual Studio doesn't do this.
If you want g++ to keep strict standard compliance (you should), use
g++ -pedantic -std=c++11 (or -std=c++14)
This should flag your program as invalid with a warning message. It is not enough to use just -std=c++xx
. Always use both options.
It is also advisable to enable various additional warning messages, for example:
g++ -Wall -Wextra -Weffc++
-Weffc++ can give you false positives, see discussion in comments
Warnings should never be ignored, but if you want the compiler to enforce this, the following recommended option makes it treat all warnings as errors:
g++ -Werror
Upvotes: 1
Reputation: 320421
Default configuration of GCC compiler is notorious for allowing non-standard features (kept for misguided "backward compatibility" with anscient code) to leak into C and even into C++ code.
If you prefer to write your code in standard C or C++ language, make sure to use such compiler settings as -pedantic
or, better, -pedantic-errors
as well as the appropriate -std=...
setting. These options are not perfect, but they will make the compiler to catch the majority of basic errors.
Upvotes: 2