C ++ Parsing

If we want to use user input to do something in the program or print the result, we need

#include <iostream> 

otherwise cout and cin will not be recognized by the compiler. However, the #include command is a pre-processor command. And when I wrote my program, the following happened. I wrote the following code:

 #define PRINT_DEBUG_INFO(a) {cout << "Info: " << a << endl;} #include <iostream> 

And no errors appeared. How can I use cout before turning on iostream ? Even if I declare PRINT_DEBUG_INFO(a) without turning on iostream , I do not get a compilation error.
Can someone explain to me why this is happening?

+8
c ++ c-preprocessor
source share
4 answers

If you just ran these 2 lines, then there is no reason for any error, because you only defined PRINT_DEBUG_INFO (a), did not implement it in any main function. A certain constant will not work if it is not implemented in the main function. Think of it as building downtime; even if it is erroneous, civil engineers will not check it for errors until they proceed with the construction of the building. A similar case is here.

However, suppose you injected it into the main function, as shown below:

 #define PRINT_DEBUG_INFO(a) {cout << a << endl;} #include <iostream> int main(void) { string a("hello"); PRINT_DEBUG_INFO(a); } 

In this case, the main function in which we implemented the function PRINT_DEBUG_INFO (a) exists after we have included the iostream file, and therefore, the program will be compiled and successfully launched.

Remember again that #define is only for defining a program; the compiler does not throw an error if a certain constant is not even implemented once; regardless of errors in it (note that syntax errors will lead to a compilation error). It will be fully checked only for errors if the function is implemented, since it is higher.

Good luck.

-2
source share

The preprocessor does not require any declared C ++ characters to be evaluated to do their job.

This is pure text processing , so defining a macro as

 #define PRINT_DEBUG_INFO(a) {cout << "Info: " << a << endl;} 

and expanding it like

 #include <iostream> void foo { int a = 5; PRINT_DEBUG_INFO(a); } 

will become

 // All the literal stuff appearing in <iostream> void foo { int a = 5; {cout << "Info: " << a << endl;}; } 

Therefore, when defining or expanding a macro, nothing is checked against the correct C ++ syntax.

These statements will be further processed by the C ++ compiler, which will complain about cout , which is not declared in the global scope.

To fix this, declare your macro as

 #define PRINT_DEBUG_INFO(a) {std::cout << "Info: " << a << std::endl;} 
+25
source share

You define PRINT_DEBUG_INFO, but you do not use it so that the compiler does not compile or complain.

+3
source share

You simply define PRINT_DEBUG_INFO(a) and do not use it. When you really use it inside your program, you will get a cout error message.

When you are not actually using it, the compiler does not find a place to replace a specific constant. When you really use it, the program expands at compile time and shows you an error.

In addition, your macro has a bracket that expands with parentheses and can lead to an error.

+2
source share

All Articles