Jay Schauer
Jay Schauer

Reputation: 487

Eclipse: Constructor Undefined

EDIT 3: Has been solved: See msandiford's answer below and my answer.

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.

Upvotes: 0

Views: 3353

Answers (2)

Jay Schauer
Jay Schauer

Reputation: 487

I have discovered the cause of the error with help from msandiford.

What happened is this: I forgot to declare a variable that was used in the return statement of the 3rd lambda expression. This caused there to be an error on the constructor that said it was undefined, which then covered up the return statement error. So I never thought to check the return statement thoroughly since it said the error was a part of the constructor, and the return statement looked fine from a distance.

In the end, I guess I learned how Eclipse errors can sometimes cover up the error that is the source.

Upvotes: 0

clstrfsck
clstrfsck

Reputation: 14837

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);

Upvotes: 2

Related Questions