亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Go 編譯器獲取“未定義:距離”錯誤

從 Go 編譯器獲取“未定義:距離”錯誤

Go
一只甜甜圈 2021-10-18 16:17:42
我是一位經驗豐富的“老派”程序員,但也是 Go 的初學者。我正在閱讀“CreateSpace:Go 編程簡介”一書。在第。111,第9章的第三章習題,任務是給用戶自定義的Shape接口添加一個新的方法。界面已經在本章的過程中建立起來了,這是我目前所擁有的:package mainimport (    "fmt"    "math")type Shape interface {    area() float64    perimeter() float64}type Distance struct {    x1, y1, x2, y2 float64}func (d *Distance) distance() float64 {    a := d.x2 - d.x1    b := d.y2 - d.y1    return math.Sqrt(a*a + b*b)}type Rectangle struct {    x1, y1, x2, y2 float64}func (r *Rectangle) area() float64 {    l := distance(r.x1, r.y1, r.x2, r.y1)    w := distance(r.x1, r.y1, r.x1, r.y2)    return l * w}type Circle struct {    x, y, r float64}func (c *Circle) area() float64 {    return math.Pi * c.r * c.r}type Perimeter struct {    x1, y1, x2, y2 float64}func (p *Perimeter) perimeter() float64 {    s1 := distance(p.x1, p.y1, p.x1, p.y2)    s2 := distance(p.x1, p.y2, p.x2, p.y2)    s3 := distance(p.x2, p.y2, p.x2, p.y1)    s4 := distance(p.x2, p.y1, p.x1, p.y1)    return s1 + s2 + s3 + s4}func main() {    d := new(Distance)    d.x2, d.y2, d.x1, d.y1 = 0, 0, 10, 10    p := new(Perimeter)    p.x1, p.y1 = 0, 0    p.x2, p.y2 = 10, 10    fmt.Println(p.perimeter())    r := new(Rectangle)    r.x1, r.y1 = 0, 0    r.x2, r.y2 = 10, 10    fmt.Println(r.area())    c := Circle{0, 0, 5}    fmt.Println(c.area())}問題是我收到以下錯誤(來自編譯器?):user@pc /c/Go/src/golang-book/chapter9/chapterProblems$ go run interface.go# command-line-arguments.\interface.go:25: undefined: distance.\interface.go:26: undefined: distance.\interface.go:42: undefined: distance.\interface.go:43: undefined: distance.\interface.go:44: undefined: distance.\interface.go:45: undefined: distance我花了很多時間重新閱讀章節文本并仔細思考這個“未定義:距離”錯誤可能意味著什么,但到目前為止無濟于事。正如你所看到的,我已經定義了一個“距離結構”,創建了一個名為 的 new() 實例,d用.運算符初始化了它的字段,并創建了一個distance()func,但是,很明顯,我不是在探索一些相關的部分) 的信息。
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

您沒有名為distance. 這是一個類型的方法*Distance。您需要先創建一個*Distance,然后調用該方法。


d := &Distance{r.x1, r.y1, r.x2, r.y1}

l := d.distance()

我建議從Effective Go開始。對于“經驗豐富的程序員”來說,這是對語言的很好的介紹。


查看完整回答
反對 回復 2021-10-18
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

您在此處定義的函數:


func (d *Distance) distance() float64 {

    a := d.x2 - d.x1

    b := d.y2 - d.y1

    return math.Sqrt(a*a + b*b)

}

是距離對象的一個方法??雌饋砟趪L試在此處創建一個新的 Distance 實例:


func (r *Rectangle) area() float64 {

    l := distance(r.x1, r.y1, r.x2, r.y1)

    w := distance(r.x1, r.y1, r.x1, r.y2)

    return l.distance() * w.distance()

}

但你實際上在做的是試圖調用一個名為distance.


你要


func (r *Rectangle) area() float64 {

    l := &Distance{r.x1, r.y1, r.x2, r.y1}

    w := &Distance{r.x1, r.y1, r.x1, r.y2}

    return l.distance() * w.distance()

}


查看完整回答
反對 回復 2021-10-18
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

在我的“頭腦正確”之后(必須修復幾個自己造成的語法錯誤),以下工作:


<pre><code>

package main


import ("fmt"; "math")


type Shape interface {

    area() float64

    perimeter() float64

}


type Distance struct {

    x1, y1, x2, y2 float64

}


func distance(x1, y1, x2, y2 float64) float64 {

    a := x2 - x1

    b := y2 - y1

    return math.Sqrt(a*a + b*b)

}


type Rectangle struct {

    x1, y1, x2, y2 float64

}


func (r *Rectangle) area() float64 {

    l  := distance(r.x1, r.y1, r.x2, r.y1)

    w := distance(r.x1, r.y1, r.x1, r.y2)

    return l * w

}

type Circle struct {

    x, y, r float64

}


func (c *Circle) area() float64 {

    return math.Pi * c.r*c.r

}


type Perimeter struct {

    x1, y1, x2, y2 float64

}


func (p *Perimeter) perimeter() float64 {

    s1 := distance(p.x1, p.y1, p.x1, p.y2)

    s2 := distance(p.x1, p.y2, p.x2, p.y2)

    s3 := distance(p.x2, p.y2, p.x2, p.y1)

    s4 := distance(p.x2, p.y1, p.x1, p.y1)

    return s1 + s2 + s3 + s4

}


func main() {

    d := new(Distance)

    d.x1, d.y1, d.x2, d.y2 = 0, 0, 10, 10


    p := new(Perimeter)

    p.x1, p.y1, p.x2, p.y2 = 0, 0, 10, 10

    fmt.Println(p.perimeter())


    r := new(Rectangle)

    r.x1, r.y1 = 0, 0

    r.x2, r.y2 = 10, 10

    fmt.Println(r.area())


    c := new(Circle)

    c.x, c.y, c.r = 0, 0, 5

    fmt.Println(c.area())


}

<pre><code>

這是結果輸出:


<pre><code>

David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems

$ go run interface.go

40

100

78.53981633974483


David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems

$

<pre><code>

再次謝謝你。


查看完整回答
反對 回復 2021-10-18
  • 3 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號