简体   繁体   English

枚举不是类的非静态数据成员或基类

[英]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. 嘿,我正在尝试编写BFS算法。我的图类看起来像这样。

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. 在struct Vertice的构造函数的初始化程序列表中,我得到以下错误颜色不是Graph :: Vertice类的非静态数据成员或基类。 I googled as much as i could but did not find anything similar to this. 我尽可能地用Google搜索,但没有找到与此类似的内容。

enum color{ WHITE, GREY, BLACK };

This will only define the values, not as one of them being instansiated inside each Vertice . 这将仅定义值,而不是在每个Vertice内部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 . 在上面的代码片段中,您声明了类型,而不是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;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 成员初始化程序“ SuperClass”未命名非静态数据成员或基类 - member initializer 'SuperClass' does not name a non-static data member or base class 该类中已删除的析构函数显示为虚拟/直接基类或非静态数据成员的类型 - Deleted destructor in the class appeared as a virtual/direct base class or as a type of non-static data member 模板类和继承问题-“列表”未命名非静态数据成员或基类 - Issues with template classes and inheritance - 'List' does not name a non-static data member or base class 类到模板错误:无效使用非静态数据成员 - Class to template error: invalid use of non-static data member class 类型的非静态数据成员的声明也是定义吗? - Is a declaration for a non-static data member of a class type also a definition? 澄清指向非静态类成员的指针 - Clarification on pointer to non-static class member 了解非静态类成员访问 - Understanding non-static class member access 将typedef作为类的非静态成员访问? - Access typedef as non-static member of class? C ++ 11非静态数据成员统一初始化失败,指针指向同一基类的其他类 - C++11 non-static data member uniform initialization fails for pointers to other classes of same base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM