C++ 模板化类常量限定符构造函数

C++ 模板化类常量限定符构造函数,c++,class,templates,binary-tree,C++,Class,Templates,Binary Tree,我无法通过“复制”相同类型的已存在对象来创建新对象 template<class dataType> inline Node<dataType>::Node(const Node<dataType> & node) { if (this != nullptr) { this->mData = node.getData(); this->mLeft = node.getLeft();

我无法通过“复制”相同类型的已存在对象来创建新对象

template<class dataType>
inline Node<dataType>::Node(const Node<dataType> & node)
{
    if (this != nullptr)
    {
        this->mData = node.getData();
        this->mLeft = node.getLeft();
        this->mRight = node.getRight();
    }
}
模板
内联节点::节点(常量节点和节点)
{
如果(此!=nullptr)
{
这->mData=node.getData();
this->mLeft=node.getLeft();
这->mRight=node.getRight();
}
}
我应该用上面的吗?或者我应该这样做:

template<class dataType>
    inline Node<dataType>::Node(const Node<dataType> & node)
    {
        this = node;
    }
模板
内联节点::节点(常量节点和节点)
{
这个=节点;
}
后者产生下一个错误:

1>h:\projects\binary search trees\data\classes\node.h(51): error C2440: '=': cannot convert from 'const Node<float> *' to 'Node<float> *const '
1>  h:\projects\binary search trees\data\classes\node.h(51): note: Conversion loses qualifiers
1>h:\projects\binary search trees\data\classes\node.h(51):错误C2440:“=”:无法从“const node*”转换为“node*const”
1> h:\projects\binary search trees\data\classes\node.h(51):注意:转换将丢失限定符
前者抱怨类似的事情:

1>h:\projects\binary search trees\data\classes\node.h(51): error C2662: 'float Node<float>::getData(void)': cannot convert 'this' pointer from 'const Node<float>' to 'Node<float> &'
1>  h:\projects\binary search trees\data\classes\node.h(51): note: Conversion loses qualifiers
1>h:\projects\binary search trees\data\classes\node.h(51):错误C2662:“float node::getData(void)”:无法将“this”指针从“const node”转换为“node&”
1> h:\projects\binary search trees\data\classes\node.h(51):注意:转换将丢失限定符

我做错了什么?

如果您已经定义了赋值运算符,您可以使用

template<class dataType>
    inline Node<dataType>::Node(const Node<dataType> & node)
    {
        *this = node;
    }
模板
内联节点::节点(常量节点和节点)
{
*这个=节点;
}
重复使用它的代码,不要重复你自己。
*
表示取消引用
指针。但您的赋值运算符必须考虑到,它可以作为左值调用默认构造值。

否,节点仅包含T mData和2个指向节点对象的指针。这是我对二叉树实现的尝试。编译器生成的构造函数不起作用。然而,这样做的目的是学习。因此,如果有人能为我指出正确的方向,我将不胜感激。您希望您的副本构造函数做什么?只需复制指针,或者从该节点向下执行整个树的深度复制?我希望它复制指针和数据,因为指针已经存储了左、右子树。