非静态成员引用必须相对于特定的 objectC/C++(245)

a nonstatic member reference must be relative to a specific objectC/C++(245)

我有以下 class 声明:

#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 */

如您所见,在 class 声明中我有几个 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;
      }

谁能告诉我如何使用我在图中定义的常量 class 而不会导致编译错误?

你应该做到static constexpr

问题是,如果没有 static,您将定义一个特定于实例的变量,如果没有 class 实例则无法访问该变量。

您可以使用 my_graph.UNDEFINED_SOURCE 或声明为静态成员

错误指出因为您没有静态成员,所以该成员应该由对象调用。

如此处所示:

#include <iostream>

class Example {
 public:
  const int my_const = 1;
  static const int my_static_const = 2;

};

int main() {
  Example a;
  std::cout << a.my_const << std::endl;
  std::cout << Example::my_static_const << std::endl;
}