Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回指向C+中对象指针的指针+; 我试图定义一组模板类来表示C++中的搜索树。 在find方法中,我需要返回包含给定键的节点的指针(表示为指针);通过这种方式,我将重用find方法来实现插入和删除 template <class K, class R> struct Node { K key; R record; inline Node(K k, R r) { this->key = k; this->record = r; } }; template <class K, class R> struct BST_Node : public Node<K,R> { BST_Node<K,R> *sx; BST_Node<K,R> *dx; inline BST_Node(K key, R record) : Node<K,R>(key, record) { this->sx = NULL; this->dx = NULL; } BST_Node<K,R> **find(K k) { BST_Node<K,R> **p = k < this->key ? &this->sx : &this->dx; while (*p && k != (*p)->key) p = k < (*p)->key ? &(*p)->sx : &(*p)->dx; return p; } /* other methods */ }; 模板 结构节点{ K键; R记录; 内联节点(K,R){ 该->键=k; 这个->记录=r; } }; 模板 结构BST_节点:公共节点{ BST_节点*sx; BST_节点*dx; 内联BST_节点(K键,R记录) :节点(键、记录){ 这->sx=NULL; 这->dx=NULL; } BST_节点**查找(K){ BST_节点**p=kkey?&this->sx:&this->dx; while(*p&&k!=(*p)->键) p=k键?&(*p)->sx:&(*p)->dx; 返回p; } /*其他方法*/ };_C++_Pointers_Binary Tree - Fatal编程技术网

返回指向C+中对象指针的指针+; 我试图定义一组模板类来表示C++中的搜索树。 在find方法中,我需要返回包含给定键的节点的指针(表示为指针);通过这种方式,我将重用find方法来实现插入和删除 template <class K, class R> struct Node { K key; R record; inline Node(K k, R r) { this->key = k; this->record = r; } }; template <class K, class R> struct BST_Node : public Node<K,R> { BST_Node<K,R> *sx; BST_Node<K,R> *dx; inline BST_Node(K key, R record) : Node<K,R>(key, record) { this->sx = NULL; this->dx = NULL; } BST_Node<K,R> **find(K k) { BST_Node<K,R> **p = k < this->key ? &this->sx : &this->dx; while (*p && k != (*p)->key) p = k < (*p)->key ? &(*p)->sx : &(*p)->dx; return p; } /* other methods */ }; 模板 结构节点{ K键; R记录; 内联节点(K,R){ 该->键=k; 这个->记录=r; } }; 模板 结构BST_节点:公共节点{ BST_节点*sx; BST_节点*dx; 内联BST_节点(K键,R记录) :节点(键、记录){ 这->sx=NULL; 这->dx=NULL; } BST_节点**查找(K){ BST_节点**p=kkey?&this->sx:&this->dx; while(*p&&k!=(*p)->键) p=k键?&(*p)->sx:&(*p)->dx; 返回p; } /*其他方法*/ };

返回指向C+中对象指针的指针+; 我试图定义一组模板类来表示C++中的搜索树。 在find方法中,我需要返回包含给定键的节点的指针(表示为指针);通过这种方式,我将重用find方法来实现插入和删除 template <class K, class R> struct Node { K key; R record; inline Node(K k, R r) { this->key = k; this->record = r; } }; template <class K, class R> struct BST_Node : public Node<K,R> { BST_Node<K,R> *sx; BST_Node<K,R> *dx; inline BST_Node(K key, R record) : Node<K,R>(key, record) { this->sx = NULL; this->dx = NULL; } BST_Node<K,R> **find(K k) { BST_Node<K,R> **p = k < this->key ? &this->sx : &this->dx; while (*p && k != (*p)->key) p = k < (*p)->key ? &(*p)->sx : &(*p)->dx; return p; } /* other methods */ }; 模板 结构节点{ K键; R记录; 内联节点(K,R){ 该->键=k; 这个->记录=r; } }; 模板 结构BST_节点:公共节点{ BST_节点*sx; BST_节点*dx; 内联BST_节点(K键,R记录) :节点(键、记录){ 这->sx=NULL; 这->dx=NULL; } BST_节点**查找(K){ BST_节点**p=kkey?&this->sx:&this->dx; while(*p&&k!=(*p)->键) p=k键?&(*p)->sx:&(*p)->dx; 返回p; } /*其他方法*/ };,c++,pointers,binary-tree,C++,Pointers,Binary Tree,有一个小问题:如果密钥在根目录中怎么办? 我不能返回&这个,那么我能做什么呢 之所以要使用指针指向指针,是因为我可以通过这种方式返回空指针的地址,因此对于插入,我可以编写如下内容: BST_Node<K,R> *insert(K k, R r) { BST_Node<K,R> **p = this->find(k); if (*p == NULL) //if the search fails *p = new BST_Node<

有一个小问题:如果密钥在根目录中怎么办?
我不能返回&这个,那么我能做什么呢

之所以要使用指针指向指针,是因为我可以通过这种方式返回空指针的地址,因此对于插入,我可以编写如下内容:

BST_Node<K,R> *insert(K k, R r) {
    BST_Node<K,R> **p = this->find(k);

    if (*p == NULL) //if the search fails
        *p = new BST_Node<K,R>(k, r);

    return *p;
}
BST_节点*插入(K,R){
BST_节点**p=this->find(k);
if(*p==NULL)//如果搜索失败
*p=新的BST_节点(k,r);
返回*p;
}

我认为你应该更仔细地重读这封信

问题是&它位于内存中的某个位置,它指向当前对象“your in”(代码正在执行的当前对象范围)。
返回,这将每次都指向你的代码当前的对象,这不是你想要的(它实际上取决于编译器,我从来没有读过编译器的任何承诺)来返回任何值,在这种情况下,它不是在C++标准)

解决方案很简单:

void* tothis = malloc(sizeof(void*)); // allocate memory that will survive leaving the current scope
tothis=this; // copy the current object memory address to the object
return &tothis; // return what you want
以后请不要忘记释放to的内存地址(这样您就不会有泄漏)。

您还不清楚。 在您的例子中,这个指针有一个类型

BST_Node<K,R>* const

提示一下,我从不在根目录下保存任何数据,通常我的树根的使用寿命与我使用树时的树根一样长。

为什么必须返回双指针?我认为没有必要返回双指针。关于双指针的原因进行了后期编辑。感谢您的解释。我编辑了我的文章,解释了使用双指针的原因。只是想澄清一下:BST_节点是一个内部类,我用它来表示一个“原始”二叉搜索树。不管怎样,你的想法很好,谢谢!
//BST_Node<K,R>**
return &this;
if (p == NULL) //if returns the root
    /*do sth.*/