如何在下面的代码中return指向children[poz]的父节点的指针?

How to return a pointer to the parent node of children[poz] in the following code?

我试图在八叉树中找到一个特定的 RGB 点(在我已经插入它之后)并且我希望这个函数 return 指向该节点父节点的指针或包含该节点及其节点的列表兄弟。我怎样才能改变这个代码来得到它?此外,当遇到空节点时,我尝试使用 returning nullptr 或 NULL,但出现编译错误:从 returned 类型 'nullptr_t' 的值到函数 return 没有可行的转换输入 'vector<Octree *>',我该如何解决?

vector<Octree*> Octree::find(int R, int G, int B)
 {
        int midR = (topLeftFront->R
                    + bottomRightBack->R)
                   / 2;
        int midG = (topLeftFront->G
                    + bottomRightBack->G)
                   / 2;
        int midB = (topLeftFront->B
                    + bottomRightBack->B)
                   / 2;

        int pos = -1;

        // Deciding the position
        // where to move
        if (R <= midR) {
            if (G <= midG) {
                if (B <= midB)
                    pos = TopLeftFront;
                else
                    pos = TopLeftBottom;
            }
            else {
                if (B <= midB)
                    pos = BottomLeftFront;
                else
                    pos = BottomLeftBack;
            }
        }
        else {
            if (G <= midG) {
                if (B <= midB)
                    pos = TopRightFront;
                else
                    pos = TopRightBottom;
            }
            else {
                if (B <= midB)
                    pos = BottomRightFront;
                else
                    pos = BottomRightBack;
            }
        }

        // If an internal node is encountered
        if (children[pos]->point == nullptr) {
            return children[pos]->find(R, G, B);
        }

        // If an empty node is encountered
        else if (children[pos]->point->R == -1) {
            return nullptr;
        }
        else {

            // If node is found with
            // the given value
            if (R == children[pos]->point->R
                && G == children[pos]->point->G
                && B == children[pos]->point->B)

            
                return children;
           
        }

    }

错误消息是一个线索。您的方法是 return 向量,因此您不能 return 指针。

您可以更改方法的 return 类型或创建 return 一个空向量。

在不知道你的数据结构的情况下,我没有关于如何 return 父级的绝对答案,但你可以在 class 中保留父级知识或传递父级作为可选参数添加到方法中。