2017-03-14 15 views
1

まずオフコード:遭遇エラー

`int main(int argc, char** argv) 
{ 
    fstream inFile; 
    inFile.open(argv[1]); 
    //where 'filename' is a string variable containing the filename 
    if (inFile.fail()) 
    { 
     cerr<<"Error opening "<<argv[1]<<endl; 
     exit(1); //terminate the program 
    } 
    else{ 
    string word; 
    AVLTree<WordStat> avl; 
    BSTree<WordStat> binary; 
    while(inFile >> word) { 
     WordStat a(word, 0); 
     avl.insert(a); 
     binary.insert(a); 
    } 

    cout << "Table 1:Binary Search Tree [" << argv[1] << "]" << endl; 
    cout << "In-order Traversal" << endl; 
    cout << "===================================" << endl; 
    cout << "Word   Frequency" << endl; 
    cout << "----------------------------------" << endl; 
    while (binary.isEmpty() != true){ 

     binary.traverse(printResults); 

    }` 

バイナリツリーの実装:

template <typename E> 
BSTree<E>::Node::Node(E s) 
{ 
    data = s; 
    left = NULL; 
    right = NULL; 
} 

/* Outer BSTree class definitions */ 

template <typename E> 
BSTree<E>::BSTree() 
{ 
    root = NULL; 
    count = 0; 
} 

template <typename E> 
BSTree<E>::~BSTree() 
{ 
    recDestroy(root); 
} 

template <typename E> 
bool BSTree<E>::isEmpty() const 
{ 
    return root == NULL; 
} 

template<typename E> 
void BSTree<E>::insert(E item) 
{ 
    Node* tmp; 
    Node* newnode = new Node(item); 
    /* If it is the first node in the tree */ 
    if (isEmpty()) 
    { 
     root = newnode; 
     count++; 
     return; 
    } 
    /*find where it should go */ 
    tmp = root; 
    while (1) 
    { 
     if (tmp->data == item) 
     { /* Key already exists. */ 
     tmp->data = item; 
     delete newnode; /* don’t need it */ 
     return; 
     } 
     else if (tmp->data > item) 
     { 
     if (!(tmp->left)) 
     { 
      /* If the key is less than tmp */ 
      tmp->left = newnode; 
      count++; 
      return; 
     } 
     else 
     { 
      /* continue searching for insertion pt. */ 
      tmp = tmp->left; 
     } 
     } 
     else 
     { 
     if (!(tmp->right)) 
     { 
      /* If the key is greater than tmp */ 
      tmp->right = newnode; 
      count++; 
      return; 
     } 
     else 
     /* continue searching for insertion point*/ 
      tmp = tmp->right; 
     } 
    } 
} 

template<typename E> 
bool BSTree<E>::inTree(E item) const 
{ 
    Node* tmp; 
    if (isEmpty()) 
     return false; 
    /*find where it is */ 
    tmp = root; 
    while (1) 
    { 
     if (tmp->data == item) 
     return true; 
     if (tmp->data > item) 
     { 
     if (!(tmp->left)) 
      return false; 
     /* continue searching */ 
     tmp = tmp->left; 
     } 
     else 
     { 
     if (!(tmp->right)) 
      return false; 
     /* continue searching for insertion pt. */ 
     tmp = tmp->right; 
     } 
    } 
} 

template<typename E> 
void BSTree<E>::remove(const E& item) 
{ 
    Node* nodeptr; 
    nodeptr = search(item); 
    if (nodeptr) 
    { 
     remove(nodeptr); 
     count--; 
    } 
} 

template<typename E> 
const E& BSTree<E>::retrieve(const E& key) const throw (BSTreeException) 
{ 
    Node* tmp; 
    if (isEmpty()) 
     throw BSTreeException("Binary Search Tree Exception: tree empty on retrieve()"); 
    tmp = root; 
    while(tmp) 
    { 
     if (tmp->data == key) 
     return tmp->data; 
     if (tmp->data > key) 
     tmp = tmp->left; 
     else 
     tmp = tmp->right; 
    } 
    if (tmp == NULL) 
     throw BSTreeException("Binary Search Tree Exception: key does not exist on retrieve()"); 
    return tmp->data; 
} 

template<typename E> 
void BSTree<E>::traverse(FuncType func) 
{ 
    traverse(root,func); 
} 

template<typename E> 
int BSTree<E>::size() const 
{ 
    return count; 
} 

template<typename E> 
int BSTree<E>::height() const 
{ 
    return height(root); 
} 

template<typename E> 
int BSTree<E>::depth(const E& item) const 
{ 
    Node* tmp; 
    tmp = root; 
    int depth; 
    while(tmp != NULL) 
    { 
     if (tmp->data == item) 
     return tmp->data; 
     if (tmp->data > item) 
     { 
     if (tmp->left == NULL) 
      break; 
     tmp = tmp->left; 
     depth++; 
     } 
     else 
     { 
     if (tmp->right == NULL) 
      break; 
     tmp = tmp->right; 
     depth++; 
     } 
    } 
    return depth; 

} 


template<typename E> 
void BSTree<E>::recDestroy(Node* root) 
{ 
    if (root) 
    { 
     if (root->left) recDestroy(root->left); 
     if (root->right) recDestroy(root->right); 
     delete root; 
    } 
} 

template<typename E> 
typename BSTree<E>::Node* BSTree<E>::findParent(Node* node) 
{ 
    Node* tmp; 
    tmp = root; 
    if (tmp == node) 
     return NULL; 
    while(1) 
    { 
     assert(tmp->data != node->data); 
     if (tmp->data > node->data) 
     { 
     assert(tmp->left != NULL); 
     if (tmp->left == node) 
      return tmp; 
     tmp = tmp->left; 
     } 
     else 
     { 
     assert(tmp->right != NULL); 
     if (tmp->right == node) 
      return tmp; 
     tmp = tmp->right; 
     } 
    } 
} 

template<typename E> 
void BSTree<E>::traverse(Node* node, FuncType func) 
{ 
    if (node) 
    { 
     traverse(node->left,func); 
     func(node->data); 
     traverse(node->right,func); 
    } 
} 


template<typename E> 
typename BSTree<E>::Node* BSTree<E>::search(const E& item) 
{ 
    Node* tmp; 
    tmp = root; 
    while(tmp) 
    { 
     if (tmp->data == item) 
     return tmp; 
     if (tmp->data > item) 
     tmp = tmp->left; 
     else 
     tmp = tmp->right; 
    } 
    return tmp; 
} 

template<typename E> 
bool BSTree<E>::remove(Node* node) 
{ 
    E data; 
    Node* parent; 
    Node* replacement; 
    parent = findParent(node); 

    if (node->left && node->right) 
    { 
     replacement = node->right; 
     while (replacement->left) 
     replacement = replacement->left; 
     data = replacement->data; 
     remove(replacement); 
     node->data = data; 
     return true; 
    } 
    else 
    { 
     if (node->left) 
      replacement = node->left; 
     else if (node->right) 
      replacement = node->right; 
     else 
      replacement = NULL; 
     if (!parent) 
     root = replacement; 
     else if (parent->left == node) 
     parent->left = replacement; 
     else 
     parent->right = replacement; 
     delete node; 
     return true; 
    } 
} 

template<typename E> 
int BSTree<E>::height(Node* node) const 
{ 
    int h = 0; 
    if (node != NULL) 
    { 
     int l_height = height(node->left); 
     int r_height = height(node->right); 
     int max_height = max(l_height, r_height); 
     h = max_height + 1; 
    } 
    return h; 
} 

template<typename E> 
void BSTree<E>::levelTraverse(FuncType func) 
{ 
    queue<E> q; 
    q.push(root); 
    while (q.empty() != true){ 
     Node *tmp = q.pop(); 
     func(tmp); 
     q.push(tmp->left); 
     q.push(tmp->right); 
    }  
} 

上記メインを実行している場合、それは横方向でエラーが発生関数は次のようにバイナリツリーで "main.cpp:67:36:error:void()(WordStat)から 'BSTree :: FuncType {別名void()(const WordStat &)}' -fertmissive] binary.traverse(printResults); "

誰かがこれを修正する上でリードを持っているのだろうかと思っていました。ありがとう!

+0

を解決すると思いますか?この関数は 'WordStat'オブジェクトを引数としてとるように見えますが、' binary.traverse(...) 'は代わりに' const WordStat& 'をとる関数へのポインタを期待しています。 – maddin45

+0

ええ、FuncTypeはprintresultsとは異なるパラメータを取るため、互換性はありません。 –

答えて

0

私はvoid printResults(const WordStat&)にprintResultsインターフェイスを変更すると、 `printResults`の定義は何の問題

関連する問題