以下是引用kai在2004-12-28 00:18:36的发言:
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
class point
{
private:
double x,y;
public:
point(double xx=0,double yy=0){x=xx;y=yy;}
friend void triangle(point &,point &,point &);
friend istream &operator>>(istream &,point &);
};
void triangle(point &p1,point &p2,point &p3)
{
double k=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
double l=sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y));
double s=sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y));
if(k+l>s && k-l<s)
{
double m=(k*k+l*l-s*s)/(2*k*l); //ÕâÀïÓ¦¸Ã×¢Òâһϣ¬Ò»¶¨ÒªÀ¨ºÅ
cout<<"Äܹ¹³ÉÈý½ÇÐΣ¬Ãæ»ýΪ£º"<<0.5*k*l*sqrt(1-m*m)<<endl;
}
else
cout<<"²»Äܹ¹³ÉÈý½ÇÐÎ"<<endl;
}
istream &operator>>(istream &input,point &p)
{
input>>p.x>>p.y; // here is an error, instead of cin, you should set the parameter input as istream object
return input;
}
int main()
{
point p1,p2,p3;
cout<<"ÇëÊäÈëÈý¸öµã£º";
cin>>p1>>p2>>p3;
triangle(p1,p2,p3);
system("pause");
return 0;
}
aa.cpp
C:\Windows\Desktop\aa.cpp(31) : error C2255: '>>' : a friend function can only be declared in a class
C:\Windows\Desktop\aa.cpp(32) : error C2248: 'x' : cannot access private member declared in class 'point'
C:\Windows\Desktop\aa.cpp(9) : see declaration of 'x'
C:\Windows\Desktop\aa.cpp(32) : error C2248: 'y' : cannot access private member declared in class 'point'
C:\Windows\Desktop\aa.cpp(9) : see declaration of 'y'
C:\Windows\Desktop\aa.cpp(33) : error C2562: '>>' : 'void' function returning a value
C:\Windows\Desktop\aa.cpp(30) : see declaration of '>>'
C:\Windows\Desktop\aa.cpp(40) : error C2593: 'operator >>' is ambiguous
Error executing cl.exe.
aa.exe - 5 error(s), 0 warning(s)
好像没用,不能访问私有成员
[此贴子已经被作者于2004-12-28 13:09:48编辑过]