main.cpp: In member function 'double Line::GetDistance()': main.cpp:56:5: error: expected initializer before 'distance' distance= sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
时间: 2024-02-20 14:00:11 浏览: 183
这个错误是因为在使用变量 `distance` 之前没有进行初始化。你需要在函数的开头给 `distance` 赋一个初始值,例如:
```
double Line::GetDistance()
{
double distance = 0.0; // 给 distance 赋初值
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
return distance;
}
```
这样就可以解决这个编译错误了。
相关问题
FormInfo class FormInfo <<partial>> [ +Formlnfo0- button1 Click(sender:obiect, e:EventArgs) : void+ SetText(run:double, walk:double) : void Form <-- FormInfo Common class Common!+ static) map left lon : double +{static) map top lat : double +{static map right lon : double +{static' map bottom lat : double class Station { +longitute :double <<get>> <<set> >+latitude :double <<get> > <<set>> + staionname :string <<get>> <<set>> + stationlD :int <<get> > <<set>> + address :string <<get>> <<set> > + totalDocks :int <<get> > <<set> > + docksln :int <<get> > < <set> > + status : string <<get> > < <set> > Station-->"screenPoint"Point Form1 class Form1 <<partial>> !-imgH:int- imgW :int isStartSite : bool isEndSite : boolI startIndex :int- endIndex :int - isPlanRoute : bool = false- m distance : double = 0 +Form10 Form1 Shown(sender:obiect,e:EventArgs) : void panelRight SizeChanged(sender:obiect,e:EventArgs): void panelButtom SizeChanged(sender:object, e:EventArgs) : voidForm1 Paint(sender:object, e:PaintEventArgs) : void buttonlmport Click(sender:object, e:EventArgs) : void Import(fileName:string): void GPSToScreen(lon:double.lat:double): PointGetTitude(point:Point): doubel listStations SelectedlndexChanged(sender:obiect, e:EventArgs) : void panel2 Paint(sender:object, e:PaintEventArgs) : voidcheckAll Click(sender:obiect, e:EventArgs) : void comboBox1 SelectedndexChanged(sender:obiect.e:EventAras): voioDrawReqularPoly(x:float, y:float, radius:double, sideCount:int) : PointF panelMouse Paint(sender:obiect,e:PaintEventArgs) : void panelMouse MouseMove(sender:object, e:MouseEventArgs) : voidpanelMouse MouseClick(sender:object, e:MouseEventArgs) : voidbutton1 Click(sender:object, e:EventArgs) : void setRoute0:void - setWalkRoute() : void - comboBox1 TextChanged(sender:object, e:EventArgs) : void class"List1"<T>{ MapHelper class MapHelper!<<const>> EarthRadius : double = 6378.137Istatic' Rad(d:double) : double +Istatic' GetDistance(firstLatitude:double.firstLongitude:double, secondLatitude:doublesecondLongitude:double): double+Istatic1 GetPointDistance(firstPoint:string,secondPoint:string): double Form <-- Form1 Form1-->"allstation<Station>""List1"Form1-->"startPoint"Point Form1-->"endPoint"Point Form1-->"startList<PointFI>""List1" "routeList<PointFIl>""List1Form1-->Form1-->"endList<PointFIl>""List1"Form1-->"m formlnfo"FormInfo画类图
根据提供的信息,可以绘制以下类图:
```
+-----------------------------------+
| FormInfo |
+-----------------------------------+
| +Formlnfo0- button1 Click(...) |
| +SetText(run:double, walk:double) |
+-----------------------------------+
+-----------------------------+
| Form |
+-----------------------------+
| <-- FormInfo |
+-----------------------------+
+-----------------------------+
| Common |
+-----------------------------+
| +{static} map left lon |
| +{static} map top lat |
| +{static} map right lon |
| +{static} map bottom lat |
+-----------------------------+
+-----------------------------+
| Station |
+-----------------------------+
| +longitude : double |
| +latitude : double |
| +stationname : string |
| +stationlD : int |
| +address : string |
| +totalDocks : int |
| +docksln : int |
| +status : string |
| --> "screenPoint" Point |
+-----------------------------+
+-----------------------------+
| Form1 |
+-----------------------------+
| <-- FormInfo |
| - imgH: int |
| - imgW: int |
| - isStartSite: bool |
| - isEndSite: bool |
| - startIndex: int |
| - endIndex: int |
| - isPlanRoute: bool = false |
| - m distance: double = 0 |
| +Form10 |
| +Form1 Shown(...) |
| +panelRight SizeChanged(...)|
| +panelButtom SizeChanged(...)|
| +Form1 Paint(...) |
| +buttonlmport Click(...) |
| +Import(fileName:string) |
| +GPSToScreen(lon:double, lat:double): Point |
| +GetTitude(point:Point): double |
| +listStations SelectedlndexChanged(...) |
| +panel2 Paint(...) |
| +checkAll Click(...) |
| +comboBox1 SelectedndexChanged(...) |
| +DrawReqularPoly(...) |
| +panelMouse Paint(...) |
| +panelMouse MouseMove(...) |
| +panelMouse MouseClick(...) |
| +button1 Click(...) |
| - setRoute0:void |
| - setWalkRoute() : void |
| - comboBox1 TextChanged(...)|
+-----------------------------+
+-----------------------------+
| List1<T> |
+-----------------------------+
+-----------------------------+
| MapHelper |
+-----------------------------+
| <<const>> EarthRadius: double = 6378.137 |
| +static Rad(d: double): double |
| +static GetDistance(firstLatitude: double, firstLongitude: double, secondLatitude: double, secondLongitude: double): double |
| +static GetPointDistance(firstPoint: string, secondPoint: string): double |
+-----------------------------+
```
请注意,这只是根据提供的信息绘制的大致类图,可能会有一些缺失或错误。
1.面向对象的应用:设计并测试一个表示一点的 MyPoint 类。 具体要求: 1) 该类包括以下私有属性: i. x:点的横坐标 ii. y:点的纵坐标 2) 包括如下方法: i. __init__()(self,x,y):构造方法,创建对象的同时为属性 x,y 赋初值; ii. getX():获得点的横坐标。 iii. getY():获得点的纵坐标。 iv. getDistance(self,p):返回当前点与点 p 之间的距离。 两点间距离公式: 3) 完成类的实例化,并计算点 P1(0,0)到点 P2(5,5)的距离,并打印出来
可以参考以下代码实现:
```python
import math
class MyPoint:
def __init__(self, x, y):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
def getDistance(self, p):
distance = math.sqrt((self.x - p.getX()) ** 2 + (self.y - p.getY()) ** 2)
return distance
p1 = MyPoint(0, 0)
p2 = MyPoint(5, 5)
distance = p1.getDistance(p2)
print("P1到P2的距离为:", distance)
```
输出结果为:
```
P1到P2的距离为: 7.0710678118654755
```
阅读全文