![]() |
#2
m21wo2010-10-30 20:34
|
这个程序有两个错误,不过我都不知道怎样改,请各位高手帮帮忙。
#include<iostream>
using namespace std;
class CLine;
class CPoint //一条直线的一个点
{
friend void CLine::GetInterPoint(CLine &line); //友元函数 这语句编译不过,我也不知道为什么,前面我已先定义了 class CLine
public:
CPoint(int x1=0,int y1=0)
{
x=x1;
y=y1;
}
public:
int x,y;
};
class CLine
{
public:
void GetInterPoint(CLine &line);
void Print() //输出直线方程
{
a=(float)(pt2.y-pt1.y)/(pt2.x-pt1.x);
b=pt1.y-a*pt1.x;
cout<<"y="<<a<<"x+"<<b<<endl;
}
CLine(CPoint&a,CPoint&b)
{
pt1.x=a.x,pt1.y=a.y;
pt2.x=b.x,pt2.y=b.y;
}
private:
CPoint pt1,pt2;
float a,b,m,n;
};
void CLine::GetInterPoint(CLine &line) //求两直线的交点(x,y)
{
m=(line.b-(*this).b)/((*this).a-line.a);
cout<<"x="<<m<<endl;
n=(line.a)*(line.b-(*this).b)/((*this).a-line.a)+line.b;
cout<<"y="<<n<<endl;
}
int main()
{
CPoint l1(2,3);
CPoint l2(5,6);
CPoint l3(1,1);
CPoint l4(4,7);
CLine line1(l1,l2);
line1.Print();
CLine line2(l3,l4);
line2.Print();
line1.GetInterPoint(CLine &line2); //说是非法调用
return 0;
}
编译器编译出现的错误:
f:\vc的程序\11-11\11-11.cpp(6) : error C2027: use of undefined type 'CLine'
f:\vc的程序\11-11\11-11.cpp(3) : see declaration of 'CLine'
f:\vc的程序\11-11\11-11.cpp(63) : error C2275: 'CLine' : illegal use of this type as an expression
f:\vc的程序\11-11\11-11.cpp(21) : see declaration of 'CLine'
执行 cl.exe 时出错.
11-11.obj - 1 error(s), 0 warning(s)