图无法解析为类型日食错误

Graph can not be resolved to a type eclipse error

我在使用 eclipse 时遇到问题,它说图形无法解析为类型,这真的很令人困惑。它是我在文本 book.The 问题的帮助下编写的大学程序的一部分,似乎无处不在,我有 DistPar、sPath 和顶点列表。谁能帮忙。

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


}

在行 Graph theGraph = new Graph(); 不能 说 "graph can nt be resolved to a type"。一定是在说 "Graph cannot be resolved to a type"。所以,欢迎来到 Java 编程;第 1 课:小写字母与大写字母 确实很重要.

(第 2 课:这不是 Eclipse 错误;这是 java 编译器发出的错误。它与 Eclipse 完全无关;它会在任何其他 IDE 还有。)

所以,编译器说 "Graph cannot be resolved to a type" 因为它不知道 "Graph" 是什么。这是很自然的,因为没有任何地方可以看到 class Graph

现在,在您的源文件中,我看到一个 public void Graph(),它正在尝试初始化您的 shortpa class 的内容,所以您可能打算这样做是调用你的 class Graph 而不是 shortpa,你的意思是 Graph() 是一个 constructor, 不是函数返回 void.

因此,将 class shortpa 替换为 class Graph,将 public void Graph() 替换为 public Graph()