2015-11-22 73 views
0

我有一個eclipse的問題,它說圖形不能解析爲一個類型,它非常混亂。它是我在一本教科書的幫助下編寫的大學課程的一部分。問題似乎無處不在,我有DistPar,sPath和頂點列表。任何人都可以幫忙圖形無法解析爲類型eclipse錯誤

public class shortpa { 

    public int distance; 
    public int parentVert; 
    public char label; 
    public boolean isInTree; 
    public final int MAX_VERTS=20; 
    public final int INFINITY=1000000; 
    public Vertex vertexList[]; 
    public int adjMat[][]; 
    public int nVerts; 
    public int nTree; 
    public DistPar sPath[]; 
    public int currentVert; 
    public int startToCurrent; 


    public static void main(String[] args) 
    { 
     Graph theGraph = new Graph();//error here saying graph can nt be resolved to a type 
     theGraph.addVertex('A'); 
     theGraph.addVertex('B'); 
     theGraph.addVertex('C'); 
     theGraph.addVertex('D'); 
     theGraph.addVertex('Z'); 


     theGraph.addEdge(1, 2, 10);//1 repesents A, 2 repesents B....etc and 10 is the weight 
     theGraph.addEdge(1, 5, 18); 
     theGraph.addEdge(2, 3, 3); 
     theGraph.addEdge(2, 5,17); 
     theGraph.addEdge(2, 4, 1); 
     theGraph.addEdge(3, 4, 1); 
     theGraph.addEdge(4, 5, 4); 

     System.out.println("Shortest paths"); 
     theGraph.path(); 
     System.out.println(); 

    } 
//--------------------------------------------------------------------------  
    public void DistPar(int pv, int d) 
    { 
     distance = d; 
     parentVert=pv; 
    } 
//--------------------------------------------------------------------------  
    public void Vertex(char lab) 
    { 
     label = lab; 
     isInTree = false; 
    } 
//--------------------------------------------------------------------------  
    public void Graph() 
    { 
     vertexList = new Vertex[MAX_VERTS];/////error here saying vertex can not be resolved to a type 
     adjMat = new int[MAX_VERTS][MAX_VERTS]; 
     nVerts=0; 
     nTree=0; 
     for(int j =0; j<MAX_VERTS; j++) 
      for(int k=0; k<MAX_VERTS; k++) 
      adjMat[j][k] = INFINITY; 
     sPath = new DistPar[MAX_VERTS];//error here saying distpar can not be resolved to a type 
    } 
//-------------------------------------------------------------------------- 
    public void addVertex(char lab) 
    { 
     vertexList[nVerts++] = new Vertex(lab); 
    } 
//------------------------------------------------------------------------- 
    public void addEdge(int start, int end, int weight)////have a look here 
    { 
     adjMat[start][end]=weight; 
    } 
//-------------------------------------------------------------------- 
    public void path() 
    { 
     int startTree = 0; 
     vertexList[startTree].isInTree = true; 
     nTree = 1; 

     for(int j=0; j<nVerts; j++) 
     { 
      int tempDist = adjMat[startTree][j]; 
      sPath[j] = new DistPar(startTree, tempDist); 
     } 
     while(nTree < nVerts) 
     { 
      int indexMin = getMin(); 
      int minDist = sPath[indexMin].distance; 

      if(minDist == INFINITY) 
      { 
       System.out.println("Unreachable vertexs"); 
       break; 
      } 
      else 
      { 
       currentVert=indexMin; 
       startToCurrent=sPath[indexMin].distance; 
      } 
      vertexList[currentVert].isInTree = true; 
      nTree++; 
      adjust_sPath(); 
     } 
     displayPaths(); 
     nTree = 0; 
     for(int j=0; j<nVerts; j++) 
      vertexList[j].isInTree = false; 
    } 
//--------------------------------------------------------------------------- 
    public int getMin() 
    { 
     int minDist = INFINITY; 
     int indexMin = 0; 
     for(int j=1; j<nVerts;j++) 
     { 
      if(!vertexList[j].isInTree && sPath[j].distance < minDist) 
      { 
       minDist = sPath[j].distance; 
       indexMin = j; 
      } 
     } 
     return indexMin; 
    } 
//------------------------------------------------------------------------------ 
    public void adjust_sPath() 
    { 
     int column = 1; 
     while(column < nVerts) 
     { 
      if(vertexList[column].isInTree) 
      { 
       column++; 
       continue; 
      } 
      int currentToFringe = adjMat[currentVert][column]; 
      int startToFringe = startToCurrent + currentToFringe; 
      int sPathDist = sPath[column].distance; 
      if(startToFringe < sPathDist) 
      { 
       sPath[column].parentVert = currentVert; 
       sPath[column].distance = startToFringe; 
      } 
      column++; 
     } 
    } 
//------------------------------------------------------------------------------ 
    public void displayPaths() 
    { 
     for(int j=0; j< nVerts; j++) 
     { 
      System.out.println(vertexList[j].label+"="); 
      if(sPath[j].distance == INFINITY) 
       System.out.println("inf"); 
      else 
       System.out.println(sPath[j].distance); 
      char parent = vertexList[sPath[j].parentVert].label; 
      System.out.println(" (" + parent + ") "); 
     } 
     System.out.println(""); 
    } 


} 

回答

1

在行Graph theGraph = new Graph();不能會說:「圖可NT解析爲一個類型」。它必須說「圖形無法解析爲某種類型」。所以,歡迎來到Java編程;第一課:小寫字母與大寫字母確實很重要

(教程#2:這不是一個Eclipse錯誤;它是由Java編譯器發出的一個錯誤它絕對無關,與Eclipse的,它會在任何其他的IDE發行以及)

因此,編譯器說「圖形不能解析爲類型」,因爲它不知道「圖形」是什麼。這是很自然的,因爲沒有任何地方可以看到class Graph

現在,在源文件中進一步下來,我看到一個public void Graph(),它試圖初始化shortpa類的內容,所以你大概的意思做的卻是打電話給你的類Graph代替shortpa,和你的意思Graph()成爲構造函數,不是返回void的函數。

因此,將class shortpa替換爲class Graph並用public Graph()替換public void Graph()

+1

謝謝,現在我的公共靜態void main不被識別。但是這有助於歡呼 – ryan1234

相關問題