mav*_*vix 1 java compiler-errors
我在尝试编译Java代码时遇到了一个我无法解释的错误:
error: constructor MinimaxThread in class MinimaxThread cannot be applied to given types;
MinimaxThread mmt = new MinimaxThread(board.clone(), 2, true);
^
required: no arguments
found: MCBoard,int,boolean
reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)
这个错误毫无意义,因为我有一个构造函数,它接受一个MCBoard,int和boolean:
public class MinimaxThread implements Runnable {
public MCBoard board;
public int depth;
public HashMap<Tuple, Integer> moveEvals;
boolean cont = true;
boolean verbose = false;
public MinimaxThread(MCBoard board, int initialDepth, boolean verbose) {
this.board = board;
depth = initialDepth;
moveEvals = new HashMap<Tuple, Integer>();
for (Tuple t : board.legalMoves) {
moveEvals.put(t, new Integer(0));
}
this.verbose = verbose;
}
Run Code Online (Sandbox Code Playgroud)
它是一个重载的构造函数(有一个只有MCBoard,一个有MCBoard和int),但我不知道这有多重要.有任何想法吗?这是调用代码:
public static void testMinimax(){
MCBoard board = new MCBoard();
board.move(5,0);
board.move(4,0);
board.move(5,2);
MinimaxThread mmt = new MinimaxThread(board.clone(), 2, true);
mmt.run();
}
Run Code Online (Sandbox Code Playgroud)
编辑:board.clone()被覆盖:
public MCBoard clone() {
// irrelevant code removed
return new MCBoard(gridClone, turn, legalMovesClone, moveListClone);
}
Run Code Online (Sandbox Code Playgroud)
编辑#2:这是我的git存储库,为了重现性:https://github.com/cowpig/MagneticCave
编辑:既然你已经给了我们你的github URL,我们可以看到MinimaxThread真正的样子 - 至少在最新的推送代码中:
public class MinimaxThread extends Thread {
public MCBoard board;
public int depth;
}
Run Code Online (Sandbox Code Playgroud)
是的,我可以看到为什么编译器会抱怨那个构造函数调用...
编辑:在我们知道MCBoard.clone()被覆盖之前,下面的答案是有道理的.但是,现在我看不出编译器应该抱怨的理由.实际上,使用你给出的代码(但忽略了实际的实现,这是无关紧要的),所有编译都很好:
MinimaxThread.java:
public class MinimaxThread implements Runnable {
public MinimaxThread(MCBoard board, int initialDepth, boolean verbose) {
}
public void run() {
}
}
Run Code Online (Sandbox Code Playgroud)
MCBoard.java:
public class MCBoard {
public MCBoard clone() {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
Test.java:
public class Test {
public static void main(String[] args) throws Exception {
MCBoard board = new MCBoard();
MinimaxThread mmt = new MinimaxThread(board.clone(), 2, true);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我怀疑你没有构建你提出的代码.尝试构建上面的代码,如果可行,请查看是否可以找出它与实际代码之间的区别.
原始答案
大概是编译器"认为" board.clone()返回Object,因为这就是声明的内容Object.clone().所以你需要将结果转换为MCBoard:
MinimaxThread mmt = new MinimaxThread((MCBoard) board.clone(), 2, true);
Run Code Online (Sandbox Code Playgroud)
或者,您可以覆盖clone()内部MCBoard,声明它返回MCBoard而不是Object.