“使用命名空间 std;"的问题 [英] issue with "using namespace std;"

查看:88

问题描述

我有一个 C++ 源代码文件.这里是:

I have a source code file in C++. Here it is:

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

我的编译器(Microsoft Visual Studio C++ 2012)给了我以下错误:

And my compiler (Microsoft Visual Studio C++ 2012) gives me the following error:

...
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2039: 'iterator_category' : is not a member of 'myPoint'
1> d:...source.cpp(5) : 见 'myPoint' 的声明
...

...
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2039: 'iterator_category' : is not a member of 'myPoint'
1> d:...source.cpp(5) : see declaration of 'myPoint'
...

当我删除 using namespace std;并更改了 cout <<距离(A, B) <<endl;std::cout <<距离(A, B) <<std::endl;我的程序成功了.

When I removed using namespace std; and changed cout << distance(A, B) << endl; to std::cout << distance(A, B) << std::endl; my program worked.

为什么第一个版本给我一个错误?什么错误?

Why does the first version give me an error? What is the mistake?

推荐答案

你和std::distance冲突

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆