Jay Schauer Jay Schauer - 6 months ago 35
Java Question

Eclipse: Constructor Undefined

I have a very strange problem in my java program that I am developing in the Eclipse IDE. I am creating an object of type "Population", using this line here:

Population population = new Population(1000, 68, 100, (Individual a, Individual b) -> {}, (Individual ind) -> {}, (Individual ind) -> {});


This causes Eclipse to detect an error:

The constructor Population(int, int, int, CrossOver, Mutate, (Individual ind) -> {}) is undefined


Then Eclipse suggests to create a new constructor as the solution to the error:

Create constructor 'Population(int, int, int, Crossover, Mutate, Fitness);


This is the constructor Eclipse produces if I click on the quick fix:

public Population(int size2, int chromosomeLength2, int generations2, CrossOver crossOver2, Mutate mutate2,
Fitness fitness2) {...}


This has the same signature as the original constructor that it says is incorrect:

public Population(int size, int chromosomeLength, int generations, CrossOver crossOver, Mutate mutate, Fitness fitness) {...}


I do not understand why this is happening, as Eclipse clearly detects what parameters are needed in a correct constructor based on the arguments I am attempting to pass, yet it does not detect that the constructor I am using is the same one that it suggests I use. So I have no clue how to remove this error, as it appears there is no error (of my own).

Also, here are the three interfaces being used in case you would like to see them as well (they do each have their own file):

public interface CrossOver {
public void crossOver(Individual a, Individual b);
}

public interface Mutate {
public void mutate(Individual a);
}

public interface Fitness {
public double getFitness(Individual ind);
}


EDIT 1: I may just have bad eyes, but in that first segment of code at the top of this post, it seems like the 2nd lambda expression's "ind" argument looks slightly different than the one in the 3rd expression. I am not sure if this carries significance in showing how Eclipse is interpreting the arguments.

EDIT 2: Actually I probably just had that in the 2nd expression selected when I copied the text.

Answer

The final lambda argument doesn't match Fitness as it doesn't return a value.

Try:

Population population = new Population(1000, 68, 100,
    (Individual a, Individual b) -> {}, (Individual ind) -> {}, (Individual ind) -> 0.0);

The types are not strictly necessary either, so you could use:

Population population = new Population(1000, 68, 100, (a, b) -> {}, ind -> {}, ind -> 0.0);
Comments