C++ 为二进制搜索树副本构造函数编写帮助函数
首先,我的树由如下节点组成:C++ 为二进制搜索树副本构造函数编写帮助函数,c++,constructor,copy,C++,Constructor,Copy,首先,我的树由如下节点组成: struct Node { string name; Node *left; //points to the left child Node *right; //points to the right child }; 对于我的复制构造函数,我有一个在根中传递的helper函数,我这样调用它(在我的复制构造函数中): 现在,对于copyHelper的主体,我需要一些帮助来复制每个节点的实际字符串 Nod
struct Node
{
string name;
Node *left; //points to the left child
Node *right; //points to the right child
};
对于我的复制构造函数,我有一个在根中传递的helper函数,我这样调用它(在我的复制构造函数中):
现在,对于copyHelper的主体,我需要一些帮助来复制每个节点的实际字符串
Node* newNode = new Node;
string newName = new string;
newName = other->name;
newNode->name = newName;
newNode->left = Helper(other->left);
newNode->right = Helper(other->right);
我是否需要在Helper中包含任何其他内容,以及为什么在堆上创建字符串时会出现该错误
字符串行上的错误为:
Error 1 error C2440: 'initializing' : cannot convert from 'std::string *' to 'std::basic_string<_Elem,_Traits,_Ax>'
错误1错误C2440:“初始化”:无法从“std::string*”转换为“std::basic_string”
正如错误消息所述,正在尝试将字符串*
分配给字符串
。要更正错误,请执行以下操作:
string newName;
不需要在堆上创建字符串
对象。此外,似乎根本没有理由使用newName
:
Node* newNode = new Node;
if (newNode)
{
newNode->name = other->name;
newNode->left = Helper(other->left);
newNode->right = Helper(other->right);
}
如错误消息所述,正在尝试将
字符串*
分配给字符串
。要更正错误,请执行以下操作:
string newName;
不需要在堆上创建字符串
对象。此外,似乎根本没有理由使用newName
:
Node* newNode = new Node;
if (newNode)
{
newNode->name = other->name;
newNode->left = Helper(other->left);
newNode->right = Helper(other->right);
}
为什么绳子不需要一个新的?为什么绳子不需要一个新的?