Search code examples
c++classobjectpointer-to-pointer

Unable to create object in C++


I've been trying to solve a Graph problem & the teacher gave me this code. I don't know how to run it. My code is:

#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

}

but it is showing me the error:

D:\Fayaz\Undirected graph.cpp|157|error: expected ';' before 'A'|

Can anyone please tell me how to create a Graph class's object and use its functions/methods? Thanks in advance...


Solution

  • Here is a pseudo-corrected version of your code:

    #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;
    }
    

    Here are some advices:

    • Prefer STL containers (safer) rather than C-style ones;
    • Use the stack every time you can (new and delete are costly). Thus forgetting to delete an object will lead to a memory leak

    I am loading the following adjacency matrix with the second constructor, Graph(const char* filename, int vertexCount):

    0 1 0 0
    0 0 1 0
    1 0 0 1
    0 0 1 1
    

    Then, I translate the adjacency matrix into a collection of arcs uv, represented by the std::map<int, std::vector<int>> graph; private member.

    Finally, I proceed to DFS.