如何从静态成员函数调用非静态成员函数
How to call non static member function from a static member function?
我需要从同一类的非静态成员函数调用非静态成员函数,例如:
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(...)
更新您可以从非静态函数调用静态方法,但反之则不然
相关文章:
- 如何在C++中使用非静态成员函数作为回调函数
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 为什么传递非静态成员函数会导致编译错误?
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 非静态成员函数的 decltype 格式不正确吗?
- std::异步与非静态成员函数
- C++无效使用非静态成员函数?
- 未使用的C++未优化的静态成员函数/变量
- C++:如何返回指向非静态成员函数的指针?
- 使用静态成员函数而不是普通函数是否有任何开销?
- 在 stream_descriptor::async_wait 中无效使用非静态成员函数
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 无法在clang Linux中分配非静态成员函数,但我可以在Visual Studio Windows中分配
- 在类定义中推导内联静态成员函数的返回类型
- `enable_if()`以禁用模板化类的静态成员函数声明
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 常量静态成员函数
- 使用 vector.sort() 的非静态成员函数无效使用
- 从静态成员函数访问私有非静态类变量 - C++