朋友。所以我在ubuntu中使用vim作爲我的編輯器創建了一個Binary Search Tree Class,當我運行我的程序時,我總是得到一個分段錯誤(core dump)錯誤。奇怪的是,當我在NetBeans上運行這個程序時,它完美運行。這是我的代碼需要幫助修復分段錯誤(核心轉儲)
#include <iostream>
using namespace std;
class BST
{
struct node {
int data;
node* left;
node* right;
};
private:
node* root;
node* addHelper(node* temp, int data)
{
if(temp == NULL)
{
temp = new node;
temp->left = temp->right = NULL;
temp->data = data;
return temp;
}
if(data < temp->data)
{
temp->left = addHelper(temp->left, data);
}
else if(data > temp->data)
{
temp->right = addHelper(temp->right, data);
}
return temp;
}
void printHelper(node* cur)
{
if(cur == NULL)
{
return;
}
else {
printHelper(cur->left);
cout << cur->data << " ";
printHelper(cur->right);
}
}
public:
void add(int value)
{
root = addHelper(root, value);
}
void printInorder()
{
printHelper(root);
}
};
int main()
{
cout << "Second Test, linux runnning sucsesfully"<<endl;
BST mytree;
mytree.add(20);
mytree.add(25);
mytree.add(10);
mytree.add(22);
mytree.add(15);
mytree.add(12);
mytree.add(23);
mytree.printInorder();
return 0;
}
我已經使用了GDB調試,並指出我的錯誤就printHelper功能,但我看不到的錯誤。如果你知道如何解決這個問題,請幫助我。 預先感謝您
還提到了初始化程序列表@Milind的調用 – CMouse