C++ 无法在C++;

C++ 无法在C++;,c++,class,object,pointer-to-pointer,C++,Class,Object,Pointer To Pointer,我一直在试图解决一个图形问题&老师给了我这个代码。我不知道如何运行它。 我的代码是: #include <bits/stdc++.h> using namespace std; #define White 0 #define Gray 2 #define Black 3 int row, col; int **Graph; class Graph { private: bool** adjacencyMatrix; int vertexCount; public

我一直在试图解决一个图形问题&老师给了我这个代码。我不知道如何运行它。 我的代码是:

#include <bits/stdc++.h>
using namespace std;

#define White 0
#define Gray 2
#define Black 3

int row, col;
int **Graph;

class Graph
{
private:
    bool** adjacencyMatrix;
    int vertexCount;
public:

    Graph(int vertexCount)
    {

        this->vertexCount = vertexCount;
        adjacencyMatrix = new bool*[vertexCount];

        for (int i = 0; i < vertexCount; i++)
        {
            adjacencyMatrix[i] = new bool[vertexCount];
            for (int j = 0; j < vertexCount; j++)
                adjacencyMatrix[i][j] = false;
        }
    }

    Graph(char filename[], int vertexCount)
    {

        this->vertexCount = vertexCount;
        adjacencyMatrix = new bool*[vertexCount];

        ifstream file;
        file.open(filename, ios::in);

        if( !file)
        {
            cout << "\nError: Cannot open file\n";
            return;
        }

        if(file.is_open())
        {
            for (int i = 0; i < vertexCount; i++)
            {

                adjacencyMatrix[i] = new bool[vertexCount];

                for (int j = 0; j < vertexCount; j++)
                    file>>adjacencyMatrix[i][j];
            }
        }
    }


    ~Graph()
    {
        for (int i = 0; i < vertexCount; i++)
            delete[] adjacencyMatrix[i];
        delete[] adjacencyMatrix;
    }

    void runDFS(int u, int state[])
    {

        state[u] = Gray;

        for (int v = 0; v < vertexCount; v++)

            if (isEdge(u, v) && state[v] == White)

                runDFS(v, state);

        state[u] = Black;
        cout<<u<<",";
    }

    void DFS()
    {

        int *state = new int[vertexCount];

        for (int i = 0; i < vertexCount; i++)
            state[i] = White;

        runDFS(0, state);

        delete [] state;

    }

    void display()
    {

        int u,v;

        for(u=0; u<vertexCount; ++u)
        {
            cout << "\nadj[" << (char) (u+65) << "] -> ";
            for(v=0; v<vertexCount; ++v)
            {
                cout << " " << adjacencyMatrix[u][v];
            }
        }
        cout << "\n\n";
    }

    bool isEdge(int i, int j)
    {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
            return adjacencyMatrix[i][j];
        else
            return false;
    }

    void removeEdge(int i, int j)
    {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
        {
            adjacencyMatrix[i][j] = false;
            adjacencyMatrix[j][i] = false;
        }
    }

    void addEdge(int i, int j)
    {
        if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
        {
            adjacencyMatrix[i][j] = true;
            adjacencyMatrix[j][i] = true;
        }
    }
};


int main()
{
    Graph = new int*[row];
    for (int i = 0; i < row; i++)
    {
        Graph[i] = new int[col];
    }

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Graph[i][j] = 0;
        }
    }

    Graph A = new Graph("Hello.txt", 2); //This line is showing the error

}
有谁能告诉我如何创建Graph类的对象并使用其函数/方法吗?
提前感谢…

以下是您代码的伪更正版本:

#include <iostream>
#include <fstream>
#include <map>
#include <vector>

enum class BWColor
{
    White,
    Gray,
    Black
};

class Graph
{
private:
    std::map<int, std::vector<int>>         graph;
    int                                     vertexCount;

private:
    void dfs(std::vector<BWColor>& visit, int u)
    {
        std::cout << u << " ";
        visit[u] = BWColor::Gray;

        for(std::vector<int>::iterator it = graph[u].begin(); it != graph[u].end(); ++it)
        {
            if(visit[*it] == BWColor::White)
            {
                dfs(visit, *it);
            }
        }

        visit[u] = BWColor::Black;
    }

public:
    Graph(int vertexCount) : vertexCount(vertexCount) {}

    Graph(const char* filename, int vertexCount) : Graph(vertexCount)
    {
        std::ifstream file;
        file.open(filename, std::ios::in);

        if(!file)
        {
            std::cerr << "Error: Cannot open file\n";
            return;
        }

        if(file.is_open())
        {
            int k = 0;
            int u = -1;

            while(file >> k)
            {
                ++u;

                if(k == 0)
                {
                    continue;
                }

                graph[u / vertexCount].push_back(u % vertexCount);
            }
        }
    }


    ~Graph() {}

    void runDFS(int u)
    {
        std::vector<BWColor> visit(vertexCount, BWColor::White);
        dfs(visit, u);
    }

    friend std::ostream& operator<<(std::ostream& stream, const Graph& g);
};

std::ostream& operator<<(std::ostream& stream, const Graph& g)
{
    for(std::map<int, std::vector<int>>::const_iterator it = g.graph.cbegin(); it != g.graph.cend(); ++it)
    {
        for(int i = 0; i < it->second.size(); ++i)
        {
            stream << '(' << it->first << ", " << it->second[i] << ")\n"; 
        }
    }

    return stream;
}

int main()
{
    Graph gA("graph.txt", 4);
    std::cout << gA << "DFS: {";
    gA.runDFS(2);
    std::cout << "}\n"; 

    return 0;
}
然后,我将邻接矩阵转换为一组弧
uv
,由
std::map图表示私人成员


最后,我转到DFS。

这里是您的代码的伪更正版本:

#include <iostream>
#include <fstream>
#include <map>
#include <vector>

enum class BWColor
{
    White,
    Gray,
    Black
};

class Graph
{
private:
    std::map<int, std::vector<int>>         graph;
    int                                     vertexCount;

private:
    void dfs(std::vector<BWColor>& visit, int u)
    {
        std::cout << u << " ";
        visit[u] = BWColor::Gray;

        for(std::vector<int>::iterator it = graph[u].begin(); it != graph[u].end(); ++it)
        {
            if(visit[*it] == BWColor::White)
            {
                dfs(visit, *it);
            }
        }

        visit[u] = BWColor::Black;
    }

public:
    Graph(int vertexCount) : vertexCount(vertexCount) {}

    Graph(const char* filename, int vertexCount) : Graph(vertexCount)
    {
        std::ifstream file;
        file.open(filename, std::ios::in);

        if(!file)
        {
            std::cerr << "Error: Cannot open file\n";
            return;
        }

        if(file.is_open())
        {
            int k = 0;
            int u = -1;

            while(file >> k)
            {
                ++u;

                if(k == 0)
                {
                    continue;
                }

                graph[u / vertexCount].push_back(u % vertexCount);
            }
        }
    }


    ~Graph() {}

    void runDFS(int u)
    {
        std::vector<BWColor> visit(vertexCount, BWColor::White);
        dfs(visit, u);
    }

    friend std::ostream& operator<<(std::ostream& stream, const Graph& g);
};

std::ostream& operator<<(std::ostream& stream, const Graph& g)
{
    for(std::map<int, std::vector<int>>::const_iterator it = g.graph.cbegin(); it != g.graph.cend(); ++it)
    {
        for(int i = 0; i < it->second.size(); ++i)
        {
            stream << '(' << it->first << ", " << it->second[i] << ")\n"; 
        }
    }

    return stream;
}

int main()
{
    Graph gA("graph.txt", 4);
    std::cout << gA << "DFS: {";
    gA.runDFS(2);
    std::cout << "}\n"; 

    return 0;
}
然后,我将邻接矩阵转换为一组弧
uv
,由
std::map图表示私人成员


最后,我继续讨论DFS。

在主菜单中,A必须是指针。您能演示一下代码吗?提前学习……为了有效地学习C++,你需要放弃使用C风格指针数组,而使用标准库工具,如:代码> STD::矢量< /代码>。那些<代码>定义< /代码>常数也迫切需要一个简单的<代码> EnUM <代码> > <代码>包含<代码> >代码>使用命名空间STD;<代码>是地狱里的一对。在这个组合中要特别小心。在你的主体中,A必须是一个指针。你能演示一下代码吗?提前学习……为了有效地学习C++,你需要放弃使用C风格指针数组,而使用标准库工具,如:代码> STD::矢量< /代码>。那些<代码>定义< /代码>常数也迫切需要一个简单的<代码> EnUM <代码> > <代码>包含<代码> >代码>使用命名空间STD;<代码>是地狱里的一对。小心组合。提前谢谢你^_^你能给我看一下你的文件的摘录吗(我想是那些代表图形的文件)?提前谢谢^_^你能给我看一下你的文件摘录吗(我想是那些代表图形的文件)?
0 1 0 0
0 0 1 0
1 0 0 1
0 0 1 1