Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么结构指针可以';不能由另一个结构指针声明_C++_Oop - Fatal编程技术网

C++ 为什么结构指针可以';不能由另一个结构指针声明

C++ 为什么结构指针可以';不能由另一个结构指针声明,c++,oop,C++,Oop,今天,我学习了二进制搜索树,我正在尝试实现它,但我遇到了一个问题 假设我有一个Struct,如下所示: struct节点{ INTV; Node*left=NULL; Node*right=NULL; } 在下面,我有: //开始时,root为NULL Node*root=NULL; 节点*新节点(int v){ Node*n=新节点; n->v=v; 返回n; } 空白插入(int v){ //在开始时,root为NULL 节点*c=根; while(c!=NULL){ 如果(vv){ c=

今天,我学习了二进制搜索树,我正在尝试实现它,但我遇到了一个问题

假设我有一个
Struct
,如下所示:

struct节点{
INTV;
Node*left=NULL;
Node*right=NULL;
}
在下面,我有:

//开始时,root为NULL
Node*root=NULL;
节点*新节点(int v){
Node*n=新节点;
n->v=v;
返回n;
}
空白插入(int v){
//在开始时,root为NULL
节点*c=根;
while(c!=NULL){
如果(vv){
c=c->左;
}否则{
c=c->右;
}
}
c=新的_节点(v);
}
在主代码中,我使用以下代码测试了我的实现:

intmain(){
插入(5);
}
当我使用
insert(5)
时,在
insert
函数中,变量
c
将是
root
,因为
root
当时是
NULL
,所以
c
将等于
新节点(v)
。但是当我打印
root->v
时,它什么也不返回


我做错了什么吗???

在您的代码中,初始化后您没有修改
根目录
root
总是
NULL
。这个

Node* c = root;
// ...
c = new_node(v);
将不会更改根目录。它只声明一个局部变量
c
,用
root
的值初始化它,并给它分配一个新值

如果你想改变函数中某个东西的值,你可以通过引用来传递它,指针在这方面没有什么不同。例如:

#include <iostream>

struct Node {
    int v;
    Node* left = NULL;
    Node* right = NULL;
};

Node* root = NULL;

Node*& find_insertion(Node*& ptr, int v){
    if (ptr == NULL) return ptr;
    if (v < ptr->v) {
        return find_insertion(ptr->left,v);
    } else {
        return find_insertion(ptr->right,v);
    }
}

void insert_at(Node*& ptr,int v){
    Node*& insertion = find_insertion(root,v);
    insertion = new Node;
    insertion->v = v;
}

void insert(int v){
    insert_at(root,v);
}

int main() {
    insert(5);
    std::cout << root->v;
}
#包括
结构节点{
INTV;
Node*left=NULL;
Node*right=NULL;
};
Node*root=NULL;
节点*&find_插入(节点*&ptr,int v){
如果(ptr==NULL)返回ptr;
如果(vv){
返回find_插入(ptr->left,v);
}否则{
返回find_插入(ptr->right,v);
}
}
在(节点*&ptr,int v)处插入空格{
节点*&插入=查找插入(根,v);
插入=新节点;
插入->v=v;
}
空白插入(int v){
在(根部,v)处插入_;
}
int main(){
插入(5);
std::cout v;
}

接下来,您应该查看智能指针(
std::unique_ptr
),以避免泄漏或编译的手动内存管理。

在代码中,初始化后不修改
根目录
root
总是
NULL
。这个

Node* c = root;
// ...
c = new_node(v);
将不会更改根目录。它只声明一个局部变量
c
,用
root
的值初始化它,并给它分配一个新值

如果你想改变函数中某个东西的值,你可以通过引用来传递它,指针在这方面没有什么不同。例如:

#include <iostream>

struct Node {
    int v;
    Node* left = NULL;
    Node* right = NULL;
};

Node* root = NULL;

Node*& find_insertion(Node*& ptr, int v){
    if (ptr == NULL) return ptr;
    if (v < ptr->v) {
        return find_insertion(ptr->left,v);
    } else {
        return find_insertion(ptr->right,v);
    }
}

void insert_at(Node*& ptr,int v){
    Node*& insertion = find_insertion(root,v);
    insertion = new Node;
    insertion->v = v;
}

void insert(int v){
    insert_at(root,v);
}

int main() {
    insert(5);
    std::cout << root->v;
}
#包括
结构节点{
INTV;
Node*left=NULL;
Node*right=NULL;
};
Node*root=NULL;
节点*&find_插入(节点*&ptr,int v){
如果(ptr==NULL)返回ptr;
如果(vv){
返回find_插入(ptr->left,v);
}否则{
返回find_插入(ptr->right,v);
}
}
在(节点*&ptr,int v)处插入空格{
节点*&插入=查找插入(根,v);
插入=新节点;
插入->v=v;
}
空白插入(int v){
在(根部,v)处插入_;
}
int main(){
插入(5);
std::cout v;
}

接下来,您应该看看智能指针(
std::unique_ptr
),以避免泄漏或编译的手动内存管理。

root
是否应该声明?在您发布的代码中没有
root
的声明请发布一个和编译器错误消息“并且因为root当时为null”,为什么?您在一个注释中写到了这一点,但是注释没有声明变量并初始化它,您必须这样做
c=new_node(v)
将修改局部变量
c
,但不修改
root
,在该行
root
仍然
NULL
@之后,该行不是素数,而是偶数。
是否应声明root
?在您发布的代码中没有
root
的声明请发布一个和编译器错误消息“并且因为root当时为null”,为什么?您在一个注释中写到了这一点,但是注释没有声明变量并初始化它,您必须这样做
c=new_node(v)
将修改局部变量
c
,但不修改
root
,在那行之后
root
仍然是
NULL
@最大的,不是素数,它是偶数。太棒了,非常感谢guy@TrầnGiaBả注意,代码被破坏了,我想我现在已经修复了,太棒了,非常感谢guy@TrầnGiaBả注意,代码被破坏了,我想我现在已经修复了