Constructor cannot be applied to given types

2019-03-04 08:06发布

问题:

been baning my head against the wall in what is probably, a simple problem that I just don't quite understand. I have these three classes, tring to pass object/methods between them.

Heres the first class

public class LargeMapDriver  
{  
    public static void main(String[] args)  
    {  
    Scanner keyboard = new Scanner(System.in);  
    int value1;  

    ThyPoint p1 = new ThyPoint(132, 734);  
    ThyPoint p2 = new ThyPoint(56, 998);  
    ThyPoint p3 = new ThyPoint(100, 105);  

    System.out.println("Enter value: ");  
    order = keyboard.nextInt();  

    LargeMap myMap = new LargeMap(value1, p1, p2, p3); 

Secondly, the pointer class.

public class ThyPoint  
{  
    private int a;  
    private int b;  

    //constructor  
    public ThyPoint(int x, int y)  
    {  
        a = x;  
        b = y;  
    }  

    //...  

    //set and get methods for a and b... not shown

    //...  

    public String toString()  
    {  
        return "a: " + getValueA() + " b: " + getValueA();  
    }  
} 

The final class, the one shows a constructor error.

public class LargeMap  
{  
    //GETTING CONSTRUCTOR(s) ERROR

    public static void goodMethod(int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3)  
    {  
    if ( value1 == 0 )  
        System.out.println( p1.toString() + p2.toString() + p3.toString());  
    else 
        System.out.println( p2.toString() + p3.toString() + p1.toString());  
     }  
} 

Ok, so the problem occurs:

**constructor LargeMap in class LargeMap cannot be applied to given types;
LargeMap myMap = new LargeMap(value1, p1, p2, p3);
                 ^
required: no arguments
found int,ThyPoint,ThyPoint,ThyPoint
reason: actual and formal arguments differ in length**

So, I'm trying to create a constructor for the LargeMap class, but failing, I'm trying to pass those values in the p1, p2, p3 objects into the constructor to accept. And to initialize the values in them, how do I do this? The values I want to initialize in them are:

    ThyPoint p1 = new ThyPoint(132, 734);  
    ThyPoint p2 = new ThyPoint(56, 998);  
    ThyPoint p3 = new ThyPoint(100, 105);  

Also the class LargeMap must remain void. It doesn't have to be static or public though.

回答1:

You are trying to use a constructor which is not declared in your "LargeMap" class
declare constructor like this instead of goodMethod() method in your "LargeMap" class

    public LargeMap(int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3)  
    {  
       if ( value1 == 0 )  
         System.out.println( p1.toString() + p2.toString() + p3.toString());  
       else 
         System.out.println( p2.toString() + p3.toString() + p1.toString());  
    } 

In your case you can access only default constructor(no argument constructor) which java compiler provides
So write your own constructor like the above.
Or you can call static method directly

    LargeMap.goodMethod(value1, p1, p2, p3); 

Instead of going for object creation.i.e instead of the below line in your code

   LargeMap myMap = new LargeMap(value1, p1, p2, p3); 


回答2:

LargeMap class doesn't have constructor which accepts four parameters at all. In case of absence of constructors, there will only be default constructor with no arguments, which is added up by Java itself.



回答3:

Perhaps posting your code for the constructor might help..

try

public LargeMap (int value1, ThyPoint p1, ThyPoint p2, ThyPoint p3) {

    //do whatever you want

}