遇到错误尝试在FuncType参数中使用Void函数

时间:2017-03-14 16:18:51

标签: c++ type-conversion

首先关闭代码:

`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时,它会导致二叉树中横向函数出错,如下所示&#34; main.cpp:67:36:错误:无效转换为&#39; void() (WordStat)&#39; to&#39; BSTree :: FuncType {aka void()(const WordStat&amp;)}&#39; [-fpermissive]         binary.traverse(printResults);&#34;

想知道是否有人有任何线索来解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:0)

我认为将printResults接口更改为void printResults(const WordStat&)将解决问题