#include<iostream>
#include<cmath>
using namespace std;
class Point;
double Distance(Point &a, Point &b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}
class Point
{
public:
Point(double xx, double yy)
{
x = xx;
y = yy;
};
void Getxy();
friend double Distance(Point &a, Point &b);
private:
double x, y;
};
void Point::Getxy()
{
cout << "(" << x << "," << y << ")" << endl;
}
int main(void)
{
Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.Getxy();
p2.Getxy();
double d = Distance(p1, p2);
cout << "Distance is" << d << endl;
return 0;
}
誰知道我這個代碼哪里不對的,我在vs中驗證通過了,但在Gcc中沒通過
uj5u.com熱心網友回復:
Distance的實作要放在Point的宣告之后我這VS也沒編譯通過啊。
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(double xx, double yy)
{
x = xx;
y = yy;
};
void Getxy();
friend double Distance(Point &a, Point &b);
private:
double x, y;
};
double Distance(Point &a, Point &b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}
void Point::Getxy()
{
cout << "(" << x << "," << y << ")" << endl;
}
int main(void)
{
Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.Getxy();
p2.Getxy();
double d = Distance(p1, p2);
cout << "Distance is" << d << endl;
return 0;
}
uj5u.com熱心網友回復:
#include<iostream>
using namespace std;
class date;
class time
{
public:
time(int,int,int);
void dispaly(date&);
private:
int hour;
int minute;
int sec;
};
class date
{
public:
date(int,int,int);
friend void time::dispaly(date &);
private:
int month;
int day;
int year;
};
time::time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void time::display(date &d)
{
cout<<d.month<<"/"<<d.day<"/"<<d.year<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
date::date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
int main()
{
time t1(10,13,56);
date d1(12,25,2004);
t1.dispaly(d1);
return 0;
}
為什么我這個編譯不過。老是出現問題,但是找不到。
uj5u.com熱心網友回復:
有拼寫錯誤 dispalycout<<d.month<<"/"<<d.day<"/"<<d.year<<endl;
少了一個<
cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78769.html
標籤:茶館
