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

问题描述

我有以下类声明:

#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 constexpr

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

,

您可以使用 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;
}