-1

I created a greedy coloring program in C++. While running this code I receive the error: identifier V is undefined on line 39 code 'int result[V];'. I have the same variable on line 46 'bool available[V];' but i don't get an error there. I think it has something to do with the vectors i used but i am not sure. Any help is greatly appreciated.

#include "stdafx.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;

class Graph
{

    vector<list<int>> adj;
public:

    Graph(int V);

    ~Graph(){}


    void addEdge(int v, int w);
    void greedyColoring(vector<bool>& available);
};

Graph::Graph(int V)
{
    adj.resize(V);

}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}

void Graph::greedyColoring(vector<bool>& available)
{
    int result[V];

    result[0] = 0;

    for (int u = 1; u < V; u++)
        result[u] = -1;

    bool available[V];
    for (int cr = 0; cr < V; cr++)
        available[cr] = false;

    for (int u = 1; u < V; u++)
    {
        list<int>::iterator i;
        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = true;

        int cr;
        for (cr = 0; cr < V; cr++)
            if (available[cr] == false)
                break;

        result[u] = cr;

        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = false;
    }

    for (int u = 0; u < V; u++)
        cout << "Vertex " << u << " ---> Color"
        << result[u] << endl;
}


int main()
{
    Graph g(6);
    g.addEdge(0, 1);
    g.addEdge(0, 4);
    g.addEdge(0, 5);
    g.addEdge(1, 3);
    g.addEdge(1, 4);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    g.addEdge(4, 5);
    cout << "Coloring of the graph \n";
    g.greedyColoring();
    return 0;
}
  • 6
    What is unclear about the error message? There is no variable called `V` in the scope of `greedyColoring` function. Do you mean `adj.size()`? Or maybe you meant to save `V` as class member in constructor? – Yksisarvinen Sep 14 '19 at 20:18
  • 1
    Variable-length arrays aren't permitted in C++. – Ben Voigt Sep 14 '19 at 20:19
  • Change `result` to `vector` and you won't need `V` – stark Sep 14 '19 at 20:21
  • 1
    Also, the fact that you don't get more errors later on doesn't mean anything. Comment out 4 lines referring to initialization of `result` and you will get an error on `bool available[V];` for sure. – Yksisarvinen Sep 14 '19 at 20:24
  • @BenVoigt spot on, I had to pass the pedantic flag to the online compiler in Wandbox.org to get this message, at least as a warning, and now I updated the answer to OP's question with this information, thank you very much. :) – gsamaras Sep 14 '19 at 20:58
  • IMHO you might benefit by using a coding standard that distinguishes 'class data attributes names' from 'local variable names'. I prefer m_v as the data attribute, and v (or sometimes lv) as the local variable. (and m_V happens to be a static class data attribute). When all your team is using the same coding standard, reading other-peoples-code becomes easier. – 2785528 Sep 14 '19 at 23:24

1 Answers1

0
  1. There is no data member of your class named V, thus when you refer to V later in your methods, there is just no such data member existing. Declare a data member of type int in your class.
  2. After that, you need to remove the parameter of the method greedyColoring(), since you define a vector available inside the body of that very same method. If you don't remove the parameter, then you would have a parameter named available shadowing an automatic variable named available. But I guess you did that in your endeavor to fix the error concerning V.
  3. Variable-length array are not Standard C++, so you cannot say int result[V];, but you can use an std::vector instead, and either resize it to V size and then use the [] operator , or push back elements as you go (in a for loop for example).

Putting everything together, you get:

#include <iostream>
#include <list>
#include <vector>
using namespace std;

class Graph
{
    int V;
    vector<list<int>> adj;
public:

    Graph(int V);

    ~Graph(){}


    void addEdge(int v, int w);
    void greedyColoring(void);
};

Graph::Graph(int V) : V(V)
{
    adj.resize(V);

}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}

void Graph::greedyColoring(void)
{
    vector<int> result;

    result.push_back(0);;
    for (int u = 1; u < V; u++)
        result.push_back(-1);

    vector<bool> available;
    for (int cr = 0; cr < V; cr++)
        available.push_back(false);

    for (int u = 1; u < V; u++)
    {
        list<int>::iterator i;
        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = true;

        int cr;
        for (cr = 0; cr < V; cr++)
            if (available[cr] == false)
                break;

        result[u] = cr;

        for (i = adj[u].begin(); i != adj[u].end(); ++i)
            if (result[*i] != -1)
                available[result[*i]] = false;
    }

    for (int u = 0; u < V; u++)
        cout << "Vertex " << u << " ---> Color"
        << result[u] << endl;
}


int main()
{
    Graph g(6);
    g.addEdge(0, 1);
    g.addEdge(0, 4);
    g.addEdge(0, 5);
    g.addEdge(1, 3);
    g.addEdge(1, 4);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    g.addEdge(4, 5);
    cout << "Coloring of the graph \n";
    g.greedyColoring();
    return 0;
}

Output:

Coloring of the graph 
Vertex 0 ---> Color0
Vertex 1 ---> Color1
Vertex 2 ---> Color0
Vertex 3 ---> Color2
Vertex 4 ---> Color2
Vertex 5 ---> Color1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I edited the code to reflect what you suggested, and now I get the error 'this' cannot be used in a constant expression on both lines 39 and 46 'int result[V];' and 'bool available[V];', – Alex Schemm Sep 14 '19 at 20:39
  • @AlexSchemm by bad, Variable length arrays are not allowed in C++, answer corrected. See the [online solution](https://wandbox.org/permlink/qRNkZaFwELZmKu52) too, if you want. :) – gsamaras Sep 14 '19 at 20:57