I’ve got the following (truncated) class declaration:
template <typename T>
class btree
{
public:
btree(const btree<T>& original); //This is btree's copy constructor
private:
struct btree_node
{
btree_node(const btree_node &other)
{
//This is node's copy constructor
}
}
btree_node* headNode;
}
And btree’s copy constructor is implemented this way:
template <typename T>
btree<T>::btree(const btree<T>& original)
{
headNode = new btree_node(original.*headNode);
}
original.*headNode is supposed to return btree_node that original.headNode is pointing to, thus matching btree_node’s copy constructor arguments.
However I get the following error:
error: ‘((btree*)this)->btree::headNode’ cannot be used as a member pointer, since it is of type ‘btree::btree_node*’
What am I doing wrong?
If you look at the precedence table you will see that first the
.operator is evaluated and then the*(dereference) operator is evaluated.*takes a pointer as argument. If you writeoriginal.*headNode, it’s meaningless. Somehow you are telling it to get the member*headNodeoforiginal, but*headNodeis not a member oforiginal. You are also telling it to evaluate*headNodewhich is actually*this->headNode(note that->is evaluated first).What you want is to first get the pointer by writing
original.headNode, then dereference it by using*. Therefore