【发布时间】:2019-11-01 10:03:06
【问题描述】:
当我调用函数 createBst() 时,程序在函数中终止。
我在函数后面放了一条打印语句,但它没有被调用。下一个打印语句“终止”没有被调用
int main(){
bst b;
b.createBst();
std::cout<<"terminated"<<std::endl;
return 0;
}
class node{
public:
int val;
node* left;
node* right;
};
class bst{
public:
node* head;
void createBst();
node* newNode(int val);
};
node* bst::newNode(int v){
node n1;
node* n=&n1;
n->val=v;
n->left=nullptr;
n->right=nullptr;
return n;
}
void bst::createBst(){
head=bst::newNode(10);
head->left=bst::newNode(11);
(head->left)->left=bst::newNode(7);
head->right=bst::newNode(9);
(head->right)->left=bst::newNode(15);
(head->right)->right=bst::newNode(8);
}
输出应该是“终止的”。
【问题讨论】:
-
类应在main中使用前定义。
标签: c++ class oop tree definition