![](/img/trans.png)
[英]Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name
[英]enum is not a non-static data member or base class of class
嘿,我正在嘗試編寫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;
};
在struct Vertice的構造函數的初始化程序列表中,我得到以下錯誤顏色不是Graph :: Vertice類的非靜態數據成員或基類。 我盡可能地用Google搜索,但沒有找到與此類似的內容。
enum color{ WHITE, GREY, BLACK };
這將僅定義值,而不是在每個Vertice
內部Vertice
其中的一個。
添加另一行以也使用一個值:
enum class Color{ WHITE, GREY, BLACK };
Color color;
Vertice* parent;
Vertice(int n) :ID(n), distance(0), color(Color::WHITE), parent(0){};
在上面的代碼片段中,您聲明了類型,而不是Vertice
的成員。 請參見下面的代碼段。
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.