user1696035
user1696035

Reputation: 1

Java - Can't find constructor?

When trying to compile, my code can't compile:

package ch02.genericStringLogs;

public class DemoGenericLogs {
  public static void main(String[] args) {
    GenericLogInterface<Float> genFloatLog = new LinkedGenericLog<Float>();
    LLGenericNode<Float> node0 = new LLGenericNode<Float>(2.2);
    LLGenericNode<Float> node1 = new LLGenericNode<Float>(3.3);
    LLGenericNode<Float> node2 = new LLGenericNode<Float>(4.4);
    LLGenericNode<Float> node3 = new LLGenericNode<Float>(5.5);
    genFloatLog.insert(node0);
    genFloatLog.insert(node1);
    genFloatLog.insert(node2);
    genFloatLog.insert(node3);

    System.out.println(genFloatLog.size());
    System.out.println(genFloatLog.toString());
    genFloatLog.clear();
    System.out.println(genFloatLog.size());

    GenericLogInterface<String> genStringLog = new LinkedGenericLog<String>();
    LLGenericNode<String> string0 = new LLGenericNode<String>("one");
    LLGenericNode<String> string1 = new LLGenericNode<String>("two");
    LLGenericNode<String> string2 = new LLGenericNode<String>("three");
    LLGenericNode<String> string3 = new LLGenericNode<String>("four");

    System.out.println(genStringLog.size());
    System.out.println(genStringLog.toString());
    genStringLog.clear();
    System.out.println(genStringLog.size());
  }
}

I get this error:

Error:
    part1/ch02/genericStringLogs/DemoGenericLogs.java:5: cannot find symbol
    symbol  : constructor LinkedGenericLog()
    location: class ch02.genericStringLogs.LinkedGenericLog<java.lang.Float>

Upvotes: 0

Views: 979

Answers (2)

rgettman
rgettman

Reputation: 178243

This line...

GenericLogInterface<String> genStringLog = new LinkedGenericLog<String>();

indicates that you are attempting to invoke a no-argument constructor.

Your LinkedGenericLog class must not have a no-argument constructor if you're getting that error. Java provides one by default, unless you define other constructors that do take arguments.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499740

Assuming it's the same class as the one in your earlier question, the only constructor for LinkedGenericLog<T> is this one:

public LinkedGenericLog(String name)

So when you construct one, you need to pass in a name. For example:

GenericLogInterface<Float> genFloatLog = new LinkedGenericLog<Float>("Some name");

If you don't want to have to pass in a name, you'll need to change LinkedGenericLog - add a parameterless constructor. What name do you want the log to have in that case though?

Upvotes: 3

Related Questions