我有一個C++源代碼文件。那就是:問題與「使用命名空間標準;」
#include <iostream>
using namespace std;
class myPoint
{
public:
double x;
double y;
myPoint() {x=y=0;}
};
double distance(myPoint A, myPoint B)
{
return (A.x - B.x);
}
int main()
{
myPoint A, B;
A.x=5; A.y=5;
B.x=3; B.y=2;
cout << distance(A, B) << endl;
return 0;
}
我的編譯器(微軟的Visual Studio C++ 2012)給了我以下錯誤:
...
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2039: 'iterator_category' : is not a member of 'myPoint'
1> d:...\source.cpp(5) : see declaration of 'myPoint'
...
當我刪除using namespace std;
,改變cout << distance(A, B) << endl;
到 std::cout << distance(A, B) << std::endl;
我的程序工作。
爲什麼第一個版本給我一個錯誤?什麼是錯誤?
請參閱http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – juanchopanza
有一個原因,標準庫將其名稱放在命名空間'std'中。吹走命名空間不是一個好主意。 –