2016-11-17 18 views
-2

在這下面的代碼宏1總是細爲什麼宏在不使用名稱空間std的同時使用名稱空間std的方式有所不同?

但是,宏2不工作的if語句1不是written..why會出現這種情況?

#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; 
} 

語句1不寫入宏2是生產錯誤,指出

'[錯誤] ENDL未在此範圍聲明爲' 如果cout<<是短版std::cout<<,這個錯誤不應該發生......我不明白爲什麼會發生這種情況......

+7

'endl'是'std'命名空間了。 – SingerOfTheFall

+0

你只是錯過了在endl之前的std ::。它應該是** std :: endl; ** – banetl

+0

所以你需要'std :: endl'。 –

回答

0

這不是你的MACRO的問題。

的ENDL對象也屬於std命名空間,所以無論是你應該使用

std::cout << std::endl; 

using namespace std; 

cout << endl; 
相關問題