简体   繁体   中英

accessing a shared pointer with a structure

This is the struct for my code I am trying to get into leftChild in a function.

struct Branch {

        shared_ptr<Node> leftChild; 
        shared_ptr<Node> middleChild; 
        shared_ptr<Node> rightChild; 


        int leftMax;
        int rightMax;

        Branch(shared_ptr<Node> left, shared_ptr<Node> middle): leftChild(left), middleChild(middle) {};
    };

my Function accepts this

Tval find_helper(Tkey k, shared_ptr<Node>& node)

I first use it here

find_helper(key, root); //though I believe this is wrong

and then I use it again recursively like so

find_helper(k, node->branch->rightChild); //also most likely wrong

I get the error: base operand of '->' has non-pointer type 'TwoThreeTree::Branch' find_helper(k, node->branch->rightChild);

what am I doing wrong in my code

EDIT (node code):

this was given to me like this

struct Node;

then using a tagged union for this:

struct Node {
        enum {BRANCH_TAG, LEAF_TAG} tag;
        union {
            Branch branch;
            Leaf leaf;
        };

        Node(shared_ptr<Node> inLeft, shared_ptr<Node> inMiddle) : tag(BRANCH_TAG), branch(Branch(inLeft, inMiddle)) {};
        Node(Tkey k, Tval v) : tag(LEAF_TAG), leaf(Leaf(k, v)) {};
        ~Node() {
            switch (tag) {
                case BRANCH_TAG:
                    branch.~Branch();
                    break;
                case LEAF_TAG:
                    leaf.~Leaf();
                    break;
            }
        }
    };

You should be using the . operator not the -> operator as your function takes in a reference, not a pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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