一个很简单的C++源程序 编译时出错 求挑错
问题:定义一个Cpoint类,用成员函数的重载实现分别求出整型数的两点间距离和实型数的两点间距离
我的源程序
#include<iostream>
#include<cmath>
using namespace std;
class Cpoint{
private:
int x1,y1,x2,y2;
double m1,n1,m2,n2;
public:
Cpoint(int a,int b,int c,int d,double e,double f,double g,double h);
int distance(int a,int b,int c,int d);
double distance(double e,double f,double g,double h);
};
Cpoint::Cpoint(int a,int b,int c,int d,double e,double f,double g,double h)
{
x1=a;
y1=b;
x2=c;
y2=d;
m1=e;
n1=f;
m2=g;
n2=h;
}
int Cpoint::distance(int a,int b,int c,int d)
{
int s,D;
s=(a-c)*(a-c)+(b-d)*(b-d);
D=sqrt(s);
return D;
}
double Cpoint::distance(double e,double f,double g,double h)
{
double s,D;
s=(e-g)*(e-g)+(f-h)*(f-h);
D=sqrt(s);
return D;
}
int main()
{
Cpoint p(0,0,0,1,0.0,0.0,0.0,1.0);
cout<<p.distance(x1,y1,x2,n2)<<endl;
cout<<p.distance(m1,n1,m2,n2)<<endl;
return 0;
}
错误提示:
c:\documents and settings\dmt01\桌面\x.cpp(42) : error C2065: 'x1' : undeclared identifier
c:\documents and settings\dmt01\桌面\x.cpp(42) : error C2065: 'x2' : undeclared identifier
c:\documents and settings\dmt01\桌面\x.cpp(42) : error C2065: 'n2' : undeclared identifier
c:\documents and settings\dmt01\桌面\x.cpp(43) : error C2065: 'm1' : undeclared identifier
c:\documents and settings\dmt01\桌面\x.cpp(43) : error C2065: 'n1' : undeclared identifier
c:\documents and settings\dmt01\桌面\x.cpp(43) : error C2065: 'm2' : undeclared identifier
我知道这是个很白痴的错误 但就是不明白 求高手找出错误 并提出改正方法
[解决办法]
cout<<p.distance(x1,y1,x2,n2)<<endl;
cout<<p.distance(m1,n1,m2,n2)<<endl;
这个能通过我剁头
成员函数调用成员变量是怎样
int Cpoint::GetX1()
{
return this->x1;
}
这个才是调用成员变量
cout<<p.distance(m1,n1,m2,n2)<<endl;
这个是什么,你想说m1,n1,m2,n2都是成员变量是么? 或许这个是你的初衷,但是他们是么?
如果x1,x2,y1,y2,m1,m2,n1,n2 这些个成员变量都是public而不是private就可以直接通过对象
p来调用,比如p.x1,p.x2 ...
之后才有
cout<<p.distance(p.x1,p.y1,p.x2,p.n2)<<endl;
cout<<p.distance(p.m1,p.n1,p.m2,p.n2)<<endl;
觉得我错了的直接砖头拍过来好么
[解决办法]
cout<<p.distance()<<endl;
cout<<p.distance(p.m1,p.n1,p.m2,p.n2)<<endl;
return 0;
}