可有人請在下面的輸出解釋:理解 '使用' 關鍵字:C++
#include <iostream>
using namespace std;
namespace A{
int x=1;
int z=2;
}
namespace B{
int y=3;
int z=4;
}
void doSomethingWith(int i) throw()
{
cout << i ;
}
void sample() throw()
{
using namespace A;
using namespace B;
doSomethingWith(x);
doSomethingWith(y);
doSomethingWith(z);
}
int main()
{
sample();
return 0;
}
輸出:
$ g++ -Wall TestCPP.cpp -o TestCPP
TestCPP.cpp: In function `void sample()':
TestCPP.cpp:26: error: `z' undeclared (first use this function)
TestCPP.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
你會期望發生什麼? –
錯誤是誤導。我認爲發佈的代碼不是你編譯的代碼,並給出了發佈的錯誤。 – Nawaz