【发布时间】:2021-07-14 13:07:06
【问题描述】:
我有以下类声明:
#ifndef ANIL_GRAPH_H
#define ANIL_GRAPH_H
#include <cstddef>
#include <iostream>
#include "anil_cursor_list.h"
namespace anil {
class graph {
private:
// Data:
cursor_list** vertices; // An array of cursor_lists whose ith element contains the neighbors of vertex i.
int* vertex_color; // An array of int whose ith element is the color (white, gray, black) of vertex i.
int* vertex_predecessor; // An array of ints whose ith element is the parent of vertex i.
int* vertex_distance; // An array of ints whose ith element is the distance from the most recent source to vertex i.
int no_of_vertices; // The number of vertices (called the order of the graph).
int no_of_edges; // The number of edges (called the size of the graph).
int most_recent_source_for_BFS; // The label of the vertex that was most recently used as a source for BFS.
// Functions:
void delete_graph();
public:
// Data:
enum vertex_color_constants {
WHITE = -3,
GRAY,
BLACK
};
const int INFINITY = -1;
const int UNDEFINED_SOURCE = -1;
const int UNDEFINED_PREDECESSOR = -1;
// Functions:
graph(int no_of_vertices);
bool is_empty();
int order_of_graph();
int size_of_graph();
int source_vertex();
int parent_vertex(int child_vertex);
int distance_to_source(int vertex);
void path_from_source(cursor_list& path_list, int vertex);
void delete_edges();
void add_edge(int vertex_u, int vertex_v);
void add_arc(int vertex_u, int vertex_v);
void BFS(int source_vertex);
friend std::ostream& operator<<(std::ostream& out, graph& rhs); // rhs = right hand side
~graph();
};
}
#endif /* ANIL_GRAPH_H */
如您所见,在类声明中我有几个const int 声明,我在实现中使用它们没有任何问题;然而,当我尝试在我的单元测试中使用它们时,我得到了这个问题标题中显示的错误。
我尝试访问UNDEFINED_SOURCE的单元测试示例:
case GRAPH_CONSTRUCTOR:
{
// Test to construct a graph.
if (verbose) {
os << "\nGRAPH_CONSTRUCTOR:" << std::endl <<
"Starting the construction operation:" <<
std::endl;
}
anil::graph my_graph(6);
if (my_graph.order_of_graph() != 6 &&
my_graph.size_of_graph() != 0 &&
my_graph.source_vertex() != anil::graph::UNDEFINED_SOURCE) {
if (verbose) {
os << "Construction unsuccessful!" << std::endl;
}
return false;
} else {
if (verbose) {
os << "Construction successful!" << std::endl;
}
return true;
}
return false;
break;
}
有人可以告诉我如何使用我在图形类中定义的常量而不会导致编译错误吗?
【问题讨论】:
-
大概,只需将这些常量设为
static成员(将该关键字添加到声明中)。我假设它们的值对于类的所有实例都是相同的。