【问题标题】:a nonstatic member reference must be relative to a specific objectC/C++(245)非静态成员引用必须相对于特定对象/C++(245)
【发布时间】: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 成员(将该关键字添加到声明中)。我假设它们的值对于类的所有实例都是相同的。

标签: c++ constants


【解决方案1】:

你应该做到static constexpr

问题是,如果没有静态,您定义的特定于实例的变量在没有类实例的情况下是无法访问的。

【讨论】:

  • 您能解释一下原因吗?
  • 关于 constexpr:您也可以将其设为 static const int,但您可能必须将定义放入您的 cpp 文件中。所以在header中声明,在cpp中定义(由于内存地址/一个定义规则)
  • 只用这个static constexpr int UNDEFINED_SOURCE = -1;就解决了问题。
【解决方案2】:

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

【讨论】:

  • 非常感谢您的详细解释:)
猜你喜欢
  • 2015-06-01
  • 2018-03-26
  • 2016-08-13
  • 2021-05-30
  • 1970-01-01
  • 2013-03-19
  • 2018-10-23
相关资源
最近更新 更多