c++ - 尝试在 FuncType 参数中使用 Void 函数时遇到错误

标签 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时,会导致二叉树中的横向函数报错如下“main.cpp:67:36: error: invalid conversion from 'void ()(WordStat)' to ' BSTree::FuncType {aka void ()(const WordStat&)}' [-fpermissive] binary.traverse(printResults);"

想知道是否有人有解决此问题的线索。谢谢!

最佳答案

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

关于c++ - 尝试在 FuncType 参数中使用 Void 函数时遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791210/

相关文章:

c++ - 在 C++ 中格式化 Cout 输出

scala - 创建包含 AnyVal 类型的数组时如何抑制从 Long 到 Double 的自动转换

java - 泛型 collections.mixin 原始类型和泛型类型。 Integer -> String - 异常但 String -> Integer 效果很好

mysql - 如何在数据库中使用整数表示货币字段

c# - 从视频中提取帧的最快方法

c++ - &vector[0] 和 vector.begin() 有什么区别?

c++ - 如何在 Internet Explorer 中显示我自己的上下文菜单

c++ - C++标准中Should和Shall的确切含义

C++ 类型转换 : error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK'

java - JOOQ强制类型代码生成