2017-11-15 13 views
-2

私のクラス割り当てはバイナリツリーを作成することです。ノードのベクトルを使用することにしました。私のコードを実行しようとすると、プロセス11の不正アクセスエラーが発生し、IDE /デバッガ(CLion)は常にノードのクラス宣言をハイライト表示します。コードは以下の通りです。誰も私がなぜこのエラーが発生し続けているのかを教えてもらえますか?無効なメモリアクセスはどこで起こっていますか?C++ EXC_BAD_ACCESS(バイナリツリー)

//Genghis Khan 
#include <iostream> 
#include <vector> 
using namespace std; 

//Creating node class and initializing tree vector 
class node 
{ 
public: 
    int key; 
    double balance; 
    string name; 
    friend class binarytree; 
    bool visited; //for use in traversal 
}; 

class binarytree 
{ 
public: 
vector<node> tree; 
int index; //index to iterate through the tree 

//Constructor 
binarytree() 
{ 
    node sentinel; 
    sentinel.key = 0; 
    sentinel.balance = 0; 
    sentinel.name = ""; 
    sentinel.visited = false; 
    tree[0] = sentinel; 
} 

//Empty 
bool empty() 
{ 
    return(tree.size() == 1); 
} 

//set temp to root 
void set() 
{ 
    index = 1; 
} 

//create a new node 
/*node create() 
{ 
    node *v = new node; 
    cout << "Enter name: "; 
    getline(cin, v->name); 
    cout << "Enter balance: "; 
    cin >> v->balance; 
    cout << "Enter key: "; 
    cin >> v->key; 
    return(*v); 
}*/ 

//return left child for an index 
int leftchild(int a) 
{ 
    return(a*2); 
} 

//return right child for an index 
int rightchild(int a) 
{ 
    return(a*2+1); 
} 
//whether a node is a leaf 
bool isleaf(int a) 
{ 
    //if there are no nodes at tree[a]'s left and right children, a is a leaf 
    try 
    { 
     cout << tree[leftchild(a)].key; 
     cout << tree[rightchild(a)].key; 
    } 
    catch(const std::invalid_argument) 
    { 
     return true; 
    } 
    return false; 
} 

//insert 
void insert(node a) 
{ 
    while(!isleaf(index)) 
    { 
     if(empty()) 
     { 
      tree[1] = a; //set root to index 1 
     } 
     else 
     { 
      if(a.key < tree[index].key) 
      { 
       index = index * 2; 
       insert(tree[index]); 
      } 
      else if(a.key > tree[index].key) 
      { 
       index = index * 2 + 1; 
       insert(tree[index]); 
      } 
     } 
    } 
    tree[index] = a; 
} 

//visit 
void visit(node a) 
{ 
    cout << a.name << endl; 
    cout << a.balance << endl; 
    cout << a.key << endl; 
} 

//inorder 
void inorder(int a) //will only pass root to this function 
{ 
    if(!isleaf(a)) 
    { 
     inorder(leftchild(a)); 
     visit(tree[a]); 
     inorder(rightchild(a)); 
    } 
} 

}; 

int main() 
{ 
    int n; 
    cout << "How many people would you like to enter: "; 
    cin >> n; 

    binarytree b; 
    node *v = new node; 
    for(int i = 0; i < n; i++) 
    { 
     cout << "Enter name: "; 
     getline(cin, v->name); 
     cout << "Enter balance: "; 
     cin >> v->balance; 
     cout << "Enter key: "; 
     cin >> v->key; 
     b.set(); 
     b.insert(*v); 
    } 

    for(int j = 0; j < b.tree.size(); j++) 
    { 
     cout << b.tree[j].key << endl; 
    } 

    return 0; 
} 
+0

を持っているの例外の詳細 – kvr

+3

'ツリーを共有してくださいする必要があり[0]'は存在しません。 –

答えて

0

例外をスローすることはありませんが、あなたが範囲外のアクセスしようとしているなら代わりにクラッシュすることがありますが、std::vector::operator[]を経由して、存在しないvector要素にアクセスしようとするあなたのコード内のさまざまな場所がありますが、要素。最初は、std::vector::at()を代わりに使用することができます。これは類似していますが、より多くの情報を提供する例外(クラッシュではなく)をスローします。

あなたはstd::vectorがどのように機能し、使用されるかについて基本的な理解が不足しているようです。例えば、binarytreeのコンストラクタでは、あなたの代わりに

tree[0] = sentinel; 

tree.push_back(sentinel); 
+0

私の教授は、単にインデックスを選択して値を割り当てると、ベクトルが自動的に動的割り当てを処理し、そのインデックスまで空のセルを作成すると教えてくれました。これが正しいかどうかは分かりませんが、どこかで言われたことでした。しかし、提案に感謝しますが、 – arnavlohe15

+0

そのような疑わしい提案は絶対に信じないでください。 – Walter

+0

"ベクトルは自動的に動的割り当てを処理し、そのインデックスまでの空のセルを作成します" - あなたが彼を理解しなかったか、彼は何も知らないばかです。 –

関連する問題