2013-02-27 187 views
0

当尝试编译,我的代码无法编译:Java - 找不到构造函数?

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

我得到这个错误:

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

正如它所说的:无法找到“LinkedGenericLog”类的无参构造函数...并且由于您没有显示该类,所以很难说更多...... – assylias 2013-02-27 22:44:09

+0

您的'LinkedGenericLog'在同一个包里有什么课? – 2013-02-27 22:45:30

+0

是的。它是在这里列出的相同的文件http://stackoverflow.com/questions/15122613/java-compile-time-error-compiler-not-recognizing-method-override – user1696035 2013-02-27 22:56:36

回答

1

这条线......

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

表明您正在尝试调用一个无参数的构造函数。

您的LinkedGenericLog类必须没有无参构造函数,如果您收到该错误。 Java provides one by default,除非你定义了其他具有参数的构造函数。

+1

或者无法访问。 – assylias 2013-02-27 22:46:30

3

假设它是相同的类作为一个在你的earlier question,为LinkedGenericLog<T>唯一的构造是这样的一个:

public LinkedGenericLog(String name) 

所以,当你构建一个,你需要一个名字来传递。例如:

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

如果你不希望有一个名字来传递,你需要改变LinkedGenericLog - 增加一个参数的构造函数。在那种情况下,你想要日志有什么名字?

+0

确定我已经把它命名为LinkedGenericLog (“节点”) 但现在我得到以下行的错误 的java:6:找不到符号 符号:构造LLGenericNode(双) 位置:类ch02.genericStringLogs.LLGenericNode < java.lang.Float> – user1696035 2013-02-27 22:58:22

+0

同样的事情。您需要一个LLGenericNode的构造函数,它需要一个double作为参数。 – 2013-02-27 23:03:53

+0

好的,但双从哪里来? – user1696035 2013-02-27 23:09:17