Java Error: The constructor is undefined

前端 未结 9 1242
庸人自扰
庸人自扰 2020-11-29 13:31

In Java, Why am I getting this error:

Error: The constructor WeightIn() is undefined

Java Code:

public class WeightIn{
  pr         


        
9条回答
  •  隐瞒了意图╮
    2020-11-29 13:48

    The compiler is encountering a call to a "WeightIn()" no argument constructor, on this line:

    WeightIn weight1 = new WeightIn();         //Error happens here.
    

    The compiler is looking for a matching constructor in the class definition, and its not finding it. That's the error. (You do have a constructor defined: "WeightIn(double,double)" but that takes two arguments, and is not match.)

    Several ways to fix this.

    The easiest is to change the code in your main method to pass two arguments.

    WeightIn weight1 = new WeightIn( 3.65, 1.7); 
    //weight1.setWeight(3.65);
    //weight2.setHeight(1.7);
    

    The calls to the setWeight and setHeight methods are redundant, since the members are already assigned values by the constructor method.

提交回复
热议问题