Typedef数组引用?

红色的

您好,我对C ++中typedef的用法有疑问。我正在尝试创建自己的图形类,在其中可以执行DFS和BFS。到目前为止,我已经附上了我上课的所有内容。但是,每次尝试编译时,都会遇到某些错误,但我不知道如何解决。我确定该错误与用于保存所有顶点的变量vertexList有关。

#include <iostream>
#include <stack>    


class myGraph{
    public:
        typedef struct Vertex{
            char label;
            bool visited;
        }Vertex;
        myGraph(int);
        void AddVertex(char);
        void addEdge(int, int);
        int adjUnvisited(int);
        void displayVertex(int);
        void dfs();    

    private:
        Vertex* vertexList;
        int** adjMatrix;
        int size;
        int vertices;
        int count;    

};    

myGraph::myGraph(int size){
    count = 0;
    size = size;
    vertices = size;
    vertexList = new Vertex[vertices];
    adjMatrix = new int*[size];
    for(int i=0; i<size; i++){
        adjMatrix[i] = new int[vertices];
    }    

    for(int i=0; i<vertices; i++){
        for(int j=0; j<vertices; j++){
            adjMatrix[i][j] = 0;
        }
    }    



}    

void myGraph::AddVertex(char label){
    Vertex* myVertex = new Vertex();
    myVertex->label = label;
    myVertex->visited = false;
    vertexList[count++] = myVertex;    

}    

void myGraph::addEdge(int a, int b){
    adjMatrix[a][b] = 1;
    adjMatrix[b][a] = 1;    

}    

int myGraph::adjUnvisited(int index){
    for(int i=0; i<vertices; i++){
        if(adjMatrix[i][index]==1 && vertexList[i]->visited==false){
            return i;
        }
    }
    return -1;
}    

void myGraph::displayVertex(int index){
    std::cout << "Current vertex: " << vertexList[index]->label << std::endl;
}    

void myGraph::dfs(){
    std::stack<int> myStack;
    int temp = 0;    

    vertexList[temp]->visited = true;    

    myStack.push(temp);    

    int unvisitedVertex;    

    while(!myStack.empty()){
        unvisitedVertex = adjUnvisited[myStack.top()];
        if(unvisitedVertex!=-1){
            myStack.push(unvisitedVertex);
            displayVertex(unvisitedVertex);
            vertexList[unvisitedVertex]->visited = true;
        }else{
            myStack.pop();
        }
    }    

}

我收到的错误消息是这样的:

没有可行的重载'='vertexList [count ++] = myVertex;

加上注释:

候选函数(隐式副本赋值运算符)不可行:第一个参数没有从'struct Vertex *'到'const myGraph :: Vertex'的已知转换;用* struct Vertex {

还有一些其他错误消息(我相信这些错误消息很小,我可以弄清楚它们):

成员引用类型'struct Vertex'不是指针;也许您打算使用'。'?if(adjMatrix [i] [index] == 1 && vertexList [i]-> visited == false){

对非静态成员函数的引用必须称为unvisitedVertex = adjUnvisited [myStack.top()];

现在我不确定我到底在做什么错,并且想知道这里有人可以帮助我吗。

非常感谢您的帮助!

约翰·伯格

您已经声明vertexList了一个指向-的指针Vertex-这很公平,因为它将是一个数组。但这意味着该数组的每个元素都是一个Vertex结构-但是您正在访问每个数组元素,就好像它是一个指针一样。

任何一个:

  • 将所有->s替换.s并在AddVertex()
  • 声明vertexListVertex **(如adjMatrix

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章