I'm trying to use the following code in C++. Can someone tell me why it is showing error?
#define def namespace;
using def std;
int main(){
return 0;
}
while the following code is working fine
#define def namespace std;
using def;
int main(){
return 0;
}
It is because of the embedded semicolon:
#define def namespace;
^
|
WHOA!
Note that #define
itself doesn't need a semicolon to terminate the definition, so if you use one it becomes part of the text that will be inserted wherever the macro is used.
After preprocessing, the first example will turn the using
line into:
using namespace; std;
which has syntactic problems, obviously.
The fix is to remove the trailing semicolon in the #define
line, like so:
#define def namespace
You should figure out how to read the pre-processed code with your compiler, it's always instructive when straightening out macro-induced confusion.