简体   繁体   中英

Using structs as paramaters and accessing their elements in a template class

I have a template class btree and have defined a struct Node in the public definitions of the header file

struct Node {
    vector<Node> chi_;
    vector<T> val_;
    Node *par_;

    Node(size_t n, Node *parent) : par_(parent) {
        chi_.reserve(n+1);
        val_.reserve(n);
    }
    ~Node() {
        chi_.clear();
        val_.clear();
    }
};

As apart of my operator= / copy constructor I want to create a recursive function 'addAll'

template <typename T>
void btree<T>::addAll(struct Node* &one, struct Node* const& two) {

    for(unsigned int a = 0; a < two.val_.size(); a++)
        one.val_.push_back(two.val_.at(a));

    for(unsigned int a = 0; a < two.chi_.size(); a++) {
        Node *newNode = new Node(max, one);
        addAll(newNode, two.chi_.at(a));
        one.chi_.push_back(newNode);
    }
}

The function declaration is weird to me - I tried something simple like btree:addAll(Node &one, const Node &two) but that produced quite a few hard to understand compiler errors, but I finally got it to accept the existence of said function by declaring it as such above + having

void addAll(struct Node*&, struct Node* const&);

in my header file.

The problem I'm having now is accessing the data elements of the Node inside my function, I get the following compile error:

btree.tem:28:23: error: request for member 'val_' in 'two', which is of non-class type 'btree<long int>::Node* const'
btree.tem:29:3: error: request for member 'val_' in 'one', which is of non-class type 'btree<long int>::Node*'
btree.tem:29:3: error: request for member 'val_' in 'two', which is of non-class type 'btree<long int>::Node* const'
btree.tem:31:23: error: request for member 'chi_' in 'two', which is of non-class type 'btree<long int>::Node* const'
btree.tem:33:3: error: request for member 'chi_' in 'two', which is of non-class type 'btree<long int>::Node* const'
btree.tem:34:3: error: request for member 'chi_' in 'one', which is of non-class type 'btree<long int>::Node*'

Not really sure if I'm declaring things correctly here or not, but this particular issue is really stumping me (coming close to a day and a half of different approaches of tinkering with it, but to no avail)

You should use -> when dereferencing a pointer to the class.

For example:

for(unsigned int a = 0; a < two->val_.size(); a++)
    one->val_.push_back(two->val_.at(a));

you have created to addall functions one that is part of you btree class and one that is just a function outside the class. You betree class is defined as a template but your node class is the one the references the type T. If you b tree can accept more then a node then add all should take a reference to a T not a node if it can only utilize the node class it shouldn't be a template.

one and two are references to pointers, so you need to use the -> operator to access the members.

one->val_.push_back(two->val_.at(a));

Also, in C++ you don't need to use the struct keyword when referring to the type.

template <typename T>
void btree<T>::addAll(Node* &one, Node* const& two)

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