val=val;this->left=nullptr;this->right=nullptr;} }; BST级{ 私人: BST_节点*根; 公众: void setBSTRoot(BST_Node*Node){root=Node;} BST_Node*getBSTRoot(){返回此->根;} }; BST创建BST(int*,int); void placeNode(BST_Node*,int); void BST_节点::setVal(int val){ 这->val=val; 考夫特; } BST_节点*BST_节点::getRight(){ 返回此->右侧; } 无效打印顺序(BST节点根){ 无法,c++,C++" /> val=val;this->left=nullptr;this->right=nullptr;} }; BST级{ 私人: BST_节点*根; 公众: void setBSTRoot(BST_Node*Node){root=Node;} BST_Node*getBSTRoot(){返回此->根;} }; BST创建BST(int*,int); void placeNode(BST_Node*,int); void BST_节点::setVal(int val){ 这->val=val; 考夫特; } BST_节点*BST_节点::getRight(){ 返回此->右侧; } 无效打印顺序(BST节点根){ 无法,c++,C++" />

Don';我不理解给定代码的输出 刚刚跳入C++,尝试运行以下代码: #include <iostream> using namespace std; class BST_Node{ private: int val; BST_Node* left; BST_Node* right; public: void setVal(int); int getVal(); void setLeft(BST_Node*); BST_Node* getLeft(); void setRight(BST_Node*); BST_Node* getRight(); BST_Node(int val){this->val=val;this->left=nullptr;this->right=nullptr;} }; class BST{ private: BST_Node* root; public: void setBSTRoot(BST_Node* node){root=node;} BST_Node* getBSTRoot(){return this->root;} }; BST createBST(int*, int); void placeNode(BST_Node*, int); void BST_Node::setVal(int val){ this->val = val; cout<<"11: "<< val << endl; } int BST_Node::getVal(){ return this->val; } void BST_Node::setLeft(BST_Node* left){ this->left = left; } void BST_Node::setRight(BST_Node* right){ this->right = right; } BST_Node* BST_Node::getLeft(){ return this->left; } BST_Node* BST_Node::getRight(){ return this->right; } void print_inorder(BST_Node root){ cout<<"working"<<endl; if(root.getLeft()!=nullptr){ print_inorder(*(root.getLeft())); } cout<<"*"<<root.getLeft()->getVal() <<"*" << endl; cout<<"*"<<root.getVal() <<"*" << endl; cout<<"*"<<root.getRight()->getVal() <<"*" << endl; } BST createBST(int* arr, int arr_size){ BST obj_bst; BST_Node* root; cout<<"0 "<<endl; root = obj_bst.getBSTRoot(); cout<<"1: "<< (*arr) << endl; //cout<<"create BST " << root->getVal() <<endl; root->setVal(*arr); cout<<"2 "<<endl; cout<<"create BST " << root->getVal() <<endl; for(int i=1;i<arr_size;i++){ cout<<"create BST " << root->getVal() <<endl; int curr_val = *(arr+i); placeNode(root,curr_val); } cout<<"create BST " << root->getVal() <<endl; return obj_bst; } void placeNode(BST_Node* root, int curr_val){ int root_val = root->getVal(); if((curr_val>root_val) && (root->getRight()!=nullptr) ){ placeNode(root->getRight(), curr_val); }else if((curr_val<=root_val) && (root->getLeft()!=nullptr)){ placeNode(root->getLeft(), curr_val); }else if((curr_val>root_val) && (root->getRight()==nullptr)){ BST_Node node(curr_val); root->setRight(&node); }else if((curr_val<=root_val) && (root->getLeft()==nullptr)){ BST_Node node(curr_val); root->setLeft(&node); }else{ cout<< "Placement denied" << endl; } } //void createBST(int* arr, int arr_size); int main(){ int arr[10] = {23,23,34,1,2,343,343,23,4343}; cout<<"working"<<endl; BST obj_bst = createBST(arr, 10);//sizeof(arr)/sizeof(int) cout<<"working"<<endl; BST_Node root = *(obj_bst.getBSTRoot()); cout<<"working"<<endl; print_inorder(root); cout<<"working"<<endl; return 0; } #包括 使用名称空间std; 类BST_节点{ 私人: int-val; BST_节点*左; BST_节点*右侧; 公众: 无效设置值(int); int getVal(); void setLeft(BST_节点*); BST_节点*getLeft(); 无效设置权限(BST_节点*); BST_节点*getRight(); BST_节点(int val){this->val=val;this->left=nullptr;this->right=nullptr;} }; BST级{ 私人: BST_节点*根; 公众: void setBSTRoot(BST_Node*Node){root=Node;} BST_Node*getBSTRoot(){返回此->根;} }; BST创建BST(int*,int); void placeNode(BST_Node*,int); void BST_节点::setVal(int val){ 这->val=val; 考夫特; } BST_节点*BST_节点::getRight(){ 返回此->右侧; } 无效打印顺序(BST节点根){ 无法

Don';我不理解给定代码的输出 刚刚跳入C++,尝试运行以下代码: #include <iostream> using namespace std; class BST_Node{ private: int val; BST_Node* left; BST_Node* right; public: void setVal(int); int getVal(); void setLeft(BST_Node*); BST_Node* getLeft(); void setRight(BST_Node*); BST_Node* getRight(); BST_Node(int val){this->val=val;this->left=nullptr;this->right=nullptr;} }; class BST{ private: BST_Node* root; public: void setBSTRoot(BST_Node* node){root=node;} BST_Node* getBSTRoot(){return this->root;} }; BST createBST(int*, int); void placeNode(BST_Node*, int); void BST_Node::setVal(int val){ this->val = val; cout<<"11: "<< val << endl; } int BST_Node::getVal(){ return this->val; } void BST_Node::setLeft(BST_Node* left){ this->left = left; } void BST_Node::setRight(BST_Node* right){ this->right = right; } BST_Node* BST_Node::getLeft(){ return this->left; } BST_Node* BST_Node::getRight(){ return this->right; } void print_inorder(BST_Node root){ cout<<"working"<<endl; if(root.getLeft()!=nullptr){ print_inorder(*(root.getLeft())); } cout<<"*"<<root.getLeft()->getVal() <<"*" << endl; cout<<"*"<<root.getVal() <<"*" << endl; cout<<"*"<<root.getRight()->getVal() <<"*" << endl; } BST createBST(int* arr, int arr_size){ BST obj_bst; BST_Node* root; cout<<"0 "<<endl; root = obj_bst.getBSTRoot(); cout<<"1: "<< (*arr) << endl; //cout<<"create BST " << root->getVal() <<endl; root->setVal(*arr); cout<<"2 "<<endl; cout<<"create BST " << root->getVal() <<endl; for(int i=1;i<arr_size;i++){ cout<<"create BST " << root->getVal() <<endl; int curr_val = *(arr+i); placeNode(root,curr_val); } cout<<"create BST " << root->getVal() <<endl; return obj_bst; } void placeNode(BST_Node* root, int curr_val){ int root_val = root->getVal(); if((curr_val>root_val) && (root->getRight()!=nullptr) ){ placeNode(root->getRight(), curr_val); }else if((curr_val<=root_val) && (root->getLeft()!=nullptr)){ placeNode(root->getLeft(), curr_val); }else if((curr_val>root_val) && (root->getRight()==nullptr)){ BST_Node node(curr_val); root->setRight(&node); }else if((curr_val<=root_val) && (root->getLeft()==nullptr)){ BST_Node node(curr_val); root->setLeft(&node); }else{ cout<< "Placement denied" << endl; } } //void createBST(int* arr, int arr_size); int main(){ int arr[10] = {23,23,34,1,2,343,343,23,4343}; cout<<"working"<<endl; BST obj_bst = createBST(arr, 10);//sizeof(arr)/sizeof(int) cout<<"working"<<endl; BST_Node root = *(obj_bst.getBSTRoot()); cout<<"working"<<endl; print_inorder(root); cout<<"working"<<endl; return 0; } #包括 使用名称空间std; 类BST_节点{ 私人: int-val; BST_节点*左; BST_节点*右侧; 公众: 无效设置值(int); int getVal(); void setLeft(BST_节点*); BST_节点*getLeft(); 无效设置权限(BST_节点*); BST_节点*getRight(); BST_节点(int val){this->val=val;this->left=nullptr;this->right=nullptr;} }; BST级{ 私人: BST_节点*根; 公众: void setBSTRoot(BST_Node*Node){root=Node;} BST_Node*getBSTRoot(){返回此->根;} }; BST创建BST(int*,int); void placeNode(BST_Node*,int); void BST_节点::setVal(int val){ 这->val=val; 考夫特; } BST_节点*BST_节点::getRight(){ 返回此->右侧; } 无效打印顺序(BST节点根){ 无法,c++,C++,在创建obj_BST后,不将值设置为BST::root。因此obj_BST.getBSTRoot()返回一个无效指针,在该指针上调用setVal,这会导致崩溃。您所说的“执行停止”是什么意思?是否存在segfault?您是否使用调试器进行了调试?尝试一次一行地执行createBST方法,您应该会找到问题的根源。为什么不查看您得到的错误?没有错误消息mate..正如@bjhend正确提到的,这是一个运行时崩溃,因为指针无效。我的compiler刚刚显示“程序在退出0时终止”:(。

在创建
obj_BST
后,不将值设置为
BST::root
。因此
obj_BST.getBSTRoot()
返回一个无效指针,在该指针上调用
setVal
,这会导致崩溃。

您所说的“执行停止”是什么意思?是否存在segfault?您是否使用调试器进行了调试?尝试一次一行地执行
createBST
方法,您应该会找到问题的根源。为什么不查看您得到的错误?没有错误消息mate..正如@bjhend正确提到的,这是一个运行时崩溃,因为指针无效。我的compiler刚刚显示“程序在退出0时终止”:(。