首页 > 解决方案 > 如何在下面的代码中返回指向children[poz]的父节点的指针?

问题描述

我试图在八叉树中找到一个特定的 RGB 点(在我已经插入它之后),我希望这个函数返回一个指向该节点的父节点的指针或一个包含该节点及其兄弟的列表。我怎样才能改变这个代码来得到它?此外,当遇到空节点时,我尝试返回 nullptr 或 NULL,但我得到一个编译错误:没有从返回类型“nullptr_t”到函数返回类型“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;
           
        }

    }

标签: c++octree

解决方案


错误消息是一个线索。你的方法返回一个向量,所以你不能返回一个指针。

您可以更改方法的返回类型,也可以创建并返回一个空向量。

在不知道数据结构的情况下,我没有关于如何返回父级的绝对答案,但是您可以在类中保留父级知识或将父级作为可选参数传递给方法。


推荐阅读