注册 登录
编程论坛 C++教室

拷贝构造函数运行不知道为什么出错

zj_1981 发布于 2008-08-22 11:56, 596 次点击
刚刚开始学c++,在拷贝函数的时候我在vc++6.0里面运行出错,不知道为什么。错误说cpp(3) : error C2871: 'std' : does not exist or is not a namespace
我觉得是#include的问题。看了很多人的拷贝构造函数,头文件都是#include <iostream>,没有.h
如果去掉using namespace std,运行就会说定义的point类失败,确实不知道为什么。。。,请高手帮帮忙解答:)
#include <iostream.h>
#include <math.h>
using namespace std;
class point
{
private:
    int X,Y;
public:
    point(int xx=0,int yy=0){X=xx;Y=yy;}
    point(point &p);
    int getx(){return X;}
    int gety(){return Y;}
};
point::point(point &p)
{
    X=p.X; Y=p.Y;
    cout<<"point拷贝构造函数被调用"<<endl;
}
class distance
{
private:
    point p1,p2;
    double dist;
public:
    distance(point xp1,point xp2);
    double getdis(){return dist;}
};
distance::distance(point xp1,point xp2):p1(xp1),p2(xp2)
{
    cout<<"distance构造函数被调用"<<endl;
    double x=double(p1.getx()-p2.getx());
    double y=double(p1.gety()-p2.gety());
    dist=(x*x+y*y);
}

void main()
{
    point myp1(1,2),myp2(4,5);
    distance myd(myp1,myp2);
    cout<<"the distance is:"<<endl;
    cout<<myd.getdis()<<endl;
}
2 回复
#2
无缘今生2008-08-22 12:57
程序的前面三行:
要么写成:
#include <iostream.h>
#include <math.h>

要么写成:
#include <iostream>
#include <math.h>
using namespace std;
#3
zj_19812008-08-22 13:19
谢谢2楼
感谢2楼,确实如你所说。不过编译的时候提示distance myd(myp1,myp2);出错
error C2872: 'distance' : ambiguous symbol
不知道是怎么回事
1