简体   繁体   English

图无法解析为日食错误

[英]Graph can not be resolved to a type eclipse error

I'm having a problem with eclipse where it says graph can not be resolved to a type, its really confusing. 我在eclipse上遇到了问题,它说图无法解析为类型,这确实令人困惑。 Its part of a college program i wrote with the help of a text book.The problem seems to be everywhere i have DistPar, sPath and vertex list. 这是我在教科书的帮助下编写的大学课程的一部分。问题似乎无处不在,我有DistPar,sPath和顶点列表。 Can anyone help. 谁能帮忙。

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


}

In the line Graph theGraph = new Graph(); Graph theGraph = new Graph();行中Graph theGraph = new Graph(); it cannot be saying "graph can nt be resolved to a type". 不能说“图形不能解析为类型”。 It must be saying "Graph cannot be resolved to a type". 必须说“图形无法解析为类型”。 So, welcome to Java programming; 因此,欢迎使用Java编程。 lesson #1: lower case letters vs. upper case letters do really matter . 第1课:小写字母与大写字母确实很重要

(Lesson #2: this is not an Eclipse error; it is an error issued by the java compiler. It has absolutely nothing to do with Eclipse; it would be issued in any other IDE as well.) (第2课:这不是Eclipse错误;它是Java编译器发出的错误。它与Eclipse完全无关;它也可以在任何其他IDE中发出。)

So, the compiler says that "Graph cannot be resolved to a type" because it does not know what "Graph" is. 因此,编译器说“图形无法解析为类型”,因为它不知道“图形”是什么。 And that is only natural, because there is no class Graph anywhere to be seen. 这是自然的,因为在任何地方都看不到class Graph

Now, further down in your source file I see a public void Graph() , which is attempting to initialize the contents of your shortpa class, so what you probably meant to do instead was to call your class Graph instead of shortpa , and you meant Graph() to be a constructor , not a function returning void. 现在,在您的源文件的更下方,我看到一个public void Graph() ,它正在尝试初始化shortpa类的内容,因此,您可能想做的是调用Graph类而不是shortpa ,您的意思是Graph()构造函数 ,而不是返回void的函数。

So, replace class shortpa with class Graph and replace public void Graph() with public Graph() . 因此,将class shortpa替换为public void Graph() class Graph ,并将public void Graph()替换为public Graph()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM