subho
subho

Reputation: 591

scala error ambiguous reference to overloaded definition

I am new to scala and trying to learn scala. I am trying to write below 2 classes but getting the below error. Please help me

scala> class Point(val x: Int,val y: Int)
defined class Point

scala> class Rectangle(val topleft: Point,val bottomright: Point){
     | def topleft: Point
     | def bottomright: Point
     | def left=topleft.x
     | def right=bottomright.x
     | def width=right-left
     | }


<console>:14: error: ambiguous reference to overloaded definition,
both value topleft in class Rectangle of type => Point
and  method topleft in class Rectangle of type => Point
match expected type ?
       def left=topleft.x
                ^


<console>:15: error: ambiguous reference to overloaded definition,
both value bottomright in class Rectangle of type => Point
and  method bottomright in class Rectangle of type => Point
match expected type ?
       def right=bottomright.x
                 ^
<console>:13: error: value bottomright is defined twice
  conflicting symbols both originated in file '<console>'
       def bottomright: Point
           ^

<console>:12: error: value topleft is defined twice
  conflicting symbols both originated in file '<console>'
       def topleft: Point

Thanks and Regards,

Upvotes: 1

Views: 930

Answers (2)

notionquest
notionquest

Reputation: 39226

Please define the Rectangle class as below without topleft and bottomright:-

scala> class Rectangle(val topleft: Point,val bottomright: Point){
     | def left=topleft.x
     | def right=bottomright.x
     | def width=right-left
     | }
defined class Rectangle

Upvotes: 2

marstran
marstran

Reputation: 28056

You have defined topleft and bottomright twice. Simply remove the following two lines to fix the error:

def topleft: Point
def bottomright: Point

Upvotes: 3

Related Questions