简体   繁体   English

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

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

I am trying to find a specific RGB point in an octree ( after I have inserted it already) and I want this function to return a pointer to that node's parent or a list with the node and its brothers.我试图在八叉树中找到一个特定的 RGB 点(在我已经插入它之后),我希望这个 function 返回指向该节点的父节点的指针或包含该节点及其兄弟的列表。 How can I change this code to get that?我怎样才能改变这个代码来得到它? Also when an empty node is encountered I tried returning nullptr or NULL and I get a compile error:no viable conversion from returned value of type 'nullptr_t' to function return type 'vector<Octree *>', how can I fix that?此外,当遇到空节点时,我尝试返回 nullptr 或 NULL,但出现编译错误:没有从“nullptr_t”类型的返回值到 function 返回类型“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;
           
        }

    }

The error message is a clue.错误消息是一个线索。 Your method is returning a vector, so you can't return a pointer.你的方法返回一个向量,所以你不能返回一个指针。

You can either change the return type of your method or create and return an empty vector.您可以更改方法的返回类型,也可以创建并返回一个空向量。

Without knowing the structure of your data, I don't have an absolute answer for how to return the parent, but you could either retain parent knowledge in your class or pass the parent to the method as an optional argument.在不知道数据结构的情况下,我没有关于如何返回父级的绝对答案,但您可以在 class 中保留父级知识,或者将父级作为可选参数传递给方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM