我不能在函数中使用新功能C++构建
I can't use new in C++ function to build
我想在此功能中为参数提供新的空间。
void Binary::PreAndMid(Qnode* root, char temp1[], char temp2[], int m, int n, int j, int k) {
if (n - m != k - j || m > n || j > k) {
return;
}
else {
root = new Qnode; /*in other function the root is become null*/
root->val = temp1[m];
cout << root->val << endl;
int f = Find(temp2, temp1[m]);
if (f == -1) {
return;
}
else {
PreAndMid(root->LC, temp1, temp2, m + 1, m + f - j, j, f - 1);
PreAndMid(root->RC, temp1, temp2, m + f - j + 1, n, f + 1, k);
}
}
}
结果是根为null。
这是因为您需要进行*& root
,以便在末尾返回新的内存地址。现在,您正在违反该规则永远不会在参数上使用分配运算符。
参数 root
作为指针的副本传递。从功能返回后,将新值分配给该指针不会延续。
如果您愿意,则需要通过参考来传递指针root
: Qnode* &root
,它将确保呼叫此功能的代码和功能本身使用相同的变量。
相关文章:
- c++20[[no.unique_address]]中的新功能是什么
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 重载算子新功能的限制
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 如何组织 git 工作流以修复错误并同时引入新功能
- 新功能"synchronized"块在C++中提供了什么优势?
- C++17 'inline variable' vs 'extern' 关键字继承自 C 的新功能
- 如何制作新功能并将其传递到另一个功能
- 我不能在函数中使用新功能C++构建
- 是否可以保证C++标准库容器调用可替换的新功能
- 向类添加新功能
- 具有新功能的 C++ 动态分配
- 避免每次要引入新功能时都使用新类
- Linux 中的新功能后没有变化
- C++11 是否支持 C11 的新功能?
- C++中的新功能语法
- 用户定义分配器的非标准库放置新功能
- 为什么 C++ 中的新功能在失败时不返回 NULL。
- 我正在使用放置新功能和虚拟功能;为什么我的虚拟功能表有误?
- linux的新功能:C++打开和关闭usb端口的问题