为二进制搜索树副本构造函数编写辅助函数
Writing a helper function for a binary search tree copy constructor
首先,我的树由如下节点组成:
struct Node
{
string name;
Node *left; //points to the left child
Node *right; //points to the right child
};
对于我的复制构造函数,我有一个在根中传递的助手函数,我这样调用它(在复制构造函数中):
root = Helper(base.root);
现在,对于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>'
正如错误消息所述,正在尝试将string*
分配给string
。纠正错误:
string newName;
不需要在堆上创建string
对象。此外,似乎根本没有理由使用newName
:
Node* newNode = new Node;
if (newNode)
{
newNode->name = other->name;
newNode->left = Helper(other->left);
newNode->right = Helper(other->right);
}
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- makefile对我的名称空间对象/函数/构造函数的不确定引用
- 没有可调用的匹配函数(构造函数),候选函数不同于对给定类型的引用
- C++库制作,隐藏在标头中没有友元的析构函数/构造函数
- 从另一个成员函数/构造函数调用C++构造函数是否执行初始值设定项列表
- 在另一个函数/构造函数 [C++] 中初始化后使变量只读
- std::调用函数时找不到函数构造函数
- 显式调用基类析构函数/构造函数合法吗
- 函数构造函数和nullptr
- 用委托构造函数构造函数
- 虚函数构造函数和调用虚函数时会发生什么
- 一些Qt函数/构造函数参数的内存管理
- 使用for_each或std::transform时,c++函函数构造函数是如何被调用的?
- std函数构造函数初始化