sharan sharan - 1 year ago 59
C++ Question

why is macro acting different while not using namespace std and while using namespace std?

in this below code macro 1 is always fine

but, macro 2 is not working if statement 1 is not written..why is this happening?

//using namespace std; //--statement 1
#define l std::cout<< //--macro 1
#define nl std::cout<<endl; //--macro 2
int main(){
l "testing";
nl // this is not working if i dont use statement 1
l "a new line";
return 0;

when statement 1 is not written macro 2 is producing a error stating that '[Error]endl was not declared in this scope'

is the short version of
, this error should not happen...i can not understand why is this happening...

Answer Source

This is not the question of your MACRO.

The endl object also belongs to the std namespace, therefore either you should use

std::cout << std::endl;


using namespace std;

cout << endl;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download