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)
oneandtwoare references to pointers, so you need to use the->operator to access the members.Also, in C++ you don’t need to use the
structkeyword when referring to the type.