2017-08-13 8 views
1

私はこれをコンパイルするのは苦労しているのですが、なぜそれが分かりません。私はネストされたクラスで何かを混乱させるようです。何らかの理由で、ByManhattanクラスはNodeにアクセスできません。誰かがエラーメッセージが意味することと一緒にその理由が説明でき、提案があれば役立つでしょう。ありがとう。入れ子ネストされたコンパレータクラス

public class Solver { 

    private class Node implements Comparable<Node>{ 
    private Board board; 
    private Node previous; 
    private int moves; 
    private int manhattan; 
    private int priority; 

    Node(Board b, Node p) { 
     board = b; 
     previous = p; 
     if (previous == null) 
      moves = 0; 
     else 
      moves = previous.moves + 1; 
     manhattan = board.manhattan(); 
     priority = moves + manhattan; 
    } 

    public int compareTo(Node that) { 
     return this.priority - that.priority; 
    } 

    // Why Doesn't this work??? 
    public Comparator<Node> manhattanOrder() { 
     Comparator<Node> m = new ByManhattan(); 
     return m; 
    } 

    private class ByManhattan implements Comparator<Node> { 
     public int compare(Node this, Node that) { // this is line 37 
      return this.manhattan- that.manhattan; 
     } 
    } 
    } 

MinPQ<Node> pq; 
ArrayList<Node> solution; 

// find a solution to the initial board (using the A* algorithm) 
public Solver(Board initial) { 
. 
. 
. 

私は取得していますエラーは次のとおりです。

Solver.java:37: error: the receiver type does not match the enclosing class type 
public int compare(Node this, Node that) { 
       ^
required: Solver.Node.ByManhattan 
found: Solver.Node 
+2

「this」は予約語です。予約語の後にパラメータの名前を付けるのは悪いことですが、名前を変える必要があります。 int compare(Node、Node)の – shockawave123

+0

の最初のパラメータを100%ではなく変更しますが、ノードクラスを静的にしてみてください。 – shockawave123

+0

ああ!ありがとうございました。複雑な質問を簡単な解決策にして申し訳ありません。ちょうど "これ"を変更した。質問は削除できますか? – Paul

答えて

3

は、あなたのパラメータ名の名前を変更します。

public int compare(Node n1, Node n2) { // this is line 37 
     return n1.manhattan- n2.manhattan; 
    } 

は、変数のために予約語thisを使用しないでください、それはコンパイルエラーです。

1

は予約語です。これがあなたのコンパイルエラーの原因です。予約語の後にパラメータ/変数の名前を付けることも悪い習慣です。名前を変える必要があります。 compare(Node,Node)

関連する問題