如何从静态成员函数调用非静态成员函数

How to call non static member function from a static member function?

本文关键字:静态成员 函数 函数调用      更新时间:2023-10-16

我需要从同一类的非静态成员函数调用非静态成员函数,例如:

  class bintree {
private:
  float root;
  bintree *left;
  bintree *right;

public:
  bintree() : left(nullptr), right(nullptr) {
  }
  bintree(const float &t) : root(t), left(nullptr), right(nullptr) {
  }
  ~bintree() {
    if (left != nullptr)
      delete left;
    if (right != nullptr)
      delete right;
  }
  static void niveles(bintree *);
  static bintree *dameBST(float** array, int depth, int left = 0, int right = -1);
  void quickSort2D(float** arr, int left, int right, int n);
};

当我调用 quickSort2D 时,我的问题是在 dameBST 函数中,程序粉碎并显示"分段错误:11"消息。

bintree *
bintree::dameBST(float **array, int depth, int left, int right)
{
  int n=0;
  depth++;
  bintree *t = new bintree;
  bintree *tl;
  bintree *tr;
  if(depth%2 != 0) { n = 1; }
  quickSort2D(array, left, right -1, n);
  if (right == -1) {right = 10;}
  if (left == right) { return nullptr; }
  if( left == right - 1 ) { return new bintree(array[left][n]); }

  int med = (left + right) / 2;
  t->root = array[med][n];
  tl = dameBST(array, depth, left, med);
  tr = dameBST(array, depth, med + 1, right);
  t->left = tl;
  t->right = tr;


return t;

我不明白为什么。

非静态成员函数是一种必须在类的实例上调用的方法。在静态上下文中没有实例。只有阶级。

如果你有类binkree的对象t,你可以调用方法quickSort2D

t.quickSort2D(...)

更新您可以从非静态函数调用静态方法,但反之则不然