c++octree

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. 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?

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;
           
        }

    }

Solution

  • 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.