sharan sharan - 20 days ago 4
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?

#include<iostream>
#include<conio.h>
//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";
getch();
return 0;
}


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

if
cout<<
is the short version of
std::cout<<
, this error should not happen...i can not understand why is this happening...

Answer

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;

or

using namespace std;

cout << endl;