简体   繁体   中英

enum is not a non-static data member or base class of class

Hey i am trying to write BFS algorithm.My graph class looks like this.

class Graph {
    struct Vertice{
        int ID;
        int distance;
        enum color{ WHITE, GREY, BLACK };
        Vertice* parent;
        Vertice(int n) :ID(n), distance(0), color(WHITE), parent(0){};
    };

public:
    Graph(int n) : adjList(n) {

    }

    void add_edge(int u, int v) {
        adjList[u - 1].insert(adjList[u].begin(),Vertice(v));
        adjList[v - 1].insert(adjList[v - 1].begin(), Vertice(u));
    }

    vector<int> shortest_reach(int start) {

    }

    vector<list<Vertice>> adjList;
};

In the initializer list of the constructor for struct Vertice i get the following error color is not a non-static data member or base class of class Graph::Vertice. I googled as much as i could but did not find anything similar to this.

enum color{ WHITE, GREY, BLACK };

This will only define the values, not as one of them being instansiated inside each Vertice .

Add another line to also use a value:

enum class Color{ WHITE, GREY, BLACK };
Color color;
Vertice* parent;
Vertice(int n) :ID(n), distance(0), color(Color::WHITE), parent(0){};

In the above code snippet you are declaring the type, not a member of the Vertice . See the code snippet below.

class Graph {

    typedef enum color_t{ WHITE, GREY, BLACK } color_t; // Declare the enum type here.

    struct Vertice{
        int ID;
        int distance;
        color_t color; // the member.

        Vertice* parent;
        Vertice(int n) :ID(n), distance(0), color(WHITE), parent(0){};
    };

public:
    Graph(int n) : adjList(n) {

    }

    void add_edge(int u, int v) {
        adjList[u - 1].insert(adjList[u].begin(),Vertice(v));
        adjList[v - 1].insert(adjList[v - 1].begin(), Vertice(u));
    }

    vector<int> shortest_reach(int start) {

    }

    vector<list<Vertice>> adjList;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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