How to take care of this error: invalid conversion from 'bool (*)(int)' to 'int'?

How to take care of this error: invalid conversion from 'bool (*)(int)' to 'int'?

我正在为通用排序列表编写此代码,并且我必须编写一个过滤器方法,但不知道我将获得哪种类型的参数。所以我就这样写了:

#include <iostream>
#include <cstring>
#include <string>
#include <functional>

#include "dummy.h"
using namespace std;

#ifndef SORT_H
#define SORT_H

template <class T>
class LinkedList {
    struct Node {
        Node(const T &in) : data(in) {}
        T data;
        Node * next;
    };

    class Iterator
    {
        Node *m_ptr;              // pointer to current node in the list
    public:
        Iterator(Node * node) {
            m_ptr = node;
        }
        Iterator& operator++() {
            m_ptr = m_ptr -> next();
            return *this;
        }
        Iterator operator++(int) {
            Iterator temp(*this);
            m_ptr = m_ptr -> next();
            return temp;
        }
        bool operator==(const Iterator other) const {
            return m_ptr==other.m_ptr; }
        bool operator!=(const Iterator other) const
        { return m_ptr!=other.m_ptr; }
        string& operator*()
        { return m_ptr->data(); }
        operator bool()
        { return m_ptr!=0; }
    };

    Node * head;

public:
    LinkedList() {
        head = nullptr;
    }
    LinkedList(T value) {
        head -> data = value;
        head -> next = nullptr;
    }

    ~LinkedList() {
        while(head != nullptr) {
            Node * n = head->next;
            delete head;
            head = n;
        }
    }
    void operator = (T &t) {
        head = t.head;
    }

//    LinkedList(LinkedList &list){
//        Node * tmp = list.head;
//        Node * curr = list.head -> next;
//        while (curr) {
//            tmp -> next = (Node*)malloc(sizeof(tmp-> next));
//            tmp -> next = curr;
//            tmp = tmp -> next;
//            curr = curr -> next;
//        }
//    }

    int length() {
        int counter = 0;
        Node * tmp = head;
        while( tmp ) {
            counter++;
            tmp = tmp -> next;
        }
        return counter;
    }

    void insert(T value) {
        if (head == nullptr) {
            head = (Node*)malloc(sizeof(head));
            head -> data = value;
            head -> next = nullptr;
            return;
        }
        Node* n = (Node*)malloc(sizeof(n));
        n -> data = value;

        Node* tmp = head;
        while (tmp != nullptr) {
           if (value > tmp -> data && tmp -> next != nullptr) {
               if (tmp -> next -> data > value) {
                   Node * curr = tmp -> next;
                   tmp -> next = n;
                   n -> next = curr;
                   return;
               } else {
                   tmp = tmp -> next;
               }
           } else if (value > tmp -> data && tmp -> next == nullptr) {
               tmp -> next = (Node*)malloc(sizeof(tmp -> next));
               n -> next = nullptr;
               tmp -> next = n;
               return;
           } else if (value == tmp -> data && tmp -> next == NULL) {
               tmp -> next = (Node*)malloc(sizeof(tmp -> next));
               n -> next = nullptr;
               tmp -> next = n;
               return;
           } else if (value == tmp -> data && tmp -> next != NULL) {
               n -> next = tmp -> next;
               tmp -> next = n;
               return;
           } else {
               n -> next = tmp;
               head = n;
               return;
           }
        }
    }

    void remove(T value) {
        Node* old = head -> next;
        free(head);
        head = old;
    }

    void print() {
        Node *curr = head;
        while (curr) {
            cout << curr->data << endl;
            curr = curr->next;
        }
    }

    Iterator begin() {
        return head->next();
    }
    Iterator end() {
        return 0;
    }
};


#endif

我的主图是这样的:

#include <iostream>
#include "sortedList.h"
#include "dummy.h"

bool func(int num) {
    if (num % 2 != 0) {
        return false;
    }
    return true;
}

int main() {
    std::cout << "Hello, World!" << std::endl;

    Dummy teeth(24);
    teeth.add(7);
    Dummy slime(11);
    slime.add(1);
    Dummy josh(32);
    LinkedList<Dummy> teeth_list;
    teeth_list.insert(teeth);
    teeth_list.insert(slime);
    teeth_list.insert(josh);
    int num = teeth_list.length();
    cout << "The length is: " << num << endl;
    teeth_list.remove(slime);
    cout << "Now printing Dummy list" << endl;
    teeth_list.insert(slime);
    LinkedList<Dummy> new_int_list;
    new_int_list = teeth_list;
    teeth_list.print();
    cout << "Now printing new_int_list" << endl;
    new_int_list.print();

    LinkedList <Dummy> dummy1;
    dummy1 = dummy1.filter(teeth_list, &func);

//    LinkedList <Dummy> dummy(teeth_list);
//    cout << "Now printing new_dummy_list" << endl;
//    new_int_list.print();


    return 0;
}

假人 class 看起来像这样:

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

#ifndef DUMB_H
#define DUMB_H

class Dummy {
    int num_of_teeth;
public:
    Dummy(int num) {
        num_of_teeth = num;
    }
    ~Dummy() {};
    void add(int num) {
        num_of_teeth += num;
    }
    void remove() {
        num_of_teeth --;
    }
    void operator = (Dummy &dumb) {
        num_of_teeth = dumb.num_of_teeth;
    }
    bool operator < ( Dummy &dumb ) {
        return num_of_teeth < dumb.num_of_teeth ? true : false;
    }

    bool operator > ( Dummy &dumb ) {
        return num_of_teeth > dumb.num_of_teeth ? true : false;
    }

    bool operator == ( Dummy &dumb ) {
        if ( dumb.num_of_teeth == num_of_teeth ) {
            return true;
        } else {
            return false;
        }
    }
    void print() {
        int num = num_of_teeth;
        while (num > 0) {
            cout << "D";
            if ((num-1) == (num_of_teeth/2)) {
                cout << "\n";
            }
            num --;
            if (num == 0) {
                cout << "\n";
            }
        }
    }
    friend ostream& operator << (ostream& output, Dummy& dumb)
    {
        output << dumb.num_of_teeth;
        return output;
    }
};

#endif

当我尝试编译它时出现错误:

   ====================[ Build | exe_name | Debug ]================================
"C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProjects\ex2.2\cmake-build-debug --target exe_name -- -j 3
Scanning dependencies of target exe_name
[ 33%] Building CXX object CMakeFiles/exe_name.dir/main.cpp.obj
In file included from C:\Users\User\CLionProjects\ex2.2\main.cpp:2:
C:\Users\User\CLionProjects\ex2.2\sortedList.h: In instantiation of 'LinkedList<T> LinkedList<T>::filter(LinkedList<T>&, B) [with B = bool (*)(int); T = Dummy]':
C:\Users\User\CLionProjects\ex2.2\main.cpp:36:45:   required from here
C:\Users\User\CLionProjects\ex2.2\sortedList.h:112:21: error: cannot convert 'Dummy' to 'int' in argument passing
             if (pred((curr -> data))) {
                 ~~~~^~~~~~~~~~~~~~~~
mingw32-make.exe[3]: *** [CMakeFiles\exe_name.dir\build.make:81: CMakeFiles/exe_name.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:94: CMakeFiles/exe_name.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:101: CMakeFiles/exe_name.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:136: exe_name] Error 2

我应该在哪里更改代码以使其工作?又如何?

**编辑: 在我将 main 中的函数更改为:

之后
bool func(Dummy num) {
    int number = num.get();
    if (number % 2 != 0) {
        return false;
    }
    return true;
}

它编译但 returns 一个无穷无尽的未知数字列表.. 喜欢-

16740248
16711872
16740296
16740248
16711872
16740296
16740248
16711872
16740296
16740248
16711872
16740296
16740248
16711872

Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

我在代码中的哪个位置尝试访问禁止的内存?还是损坏的?

您在调用 pred((curr -> data) 时将整个 Dummy 传递给谓词函数。 pred,来自行 dummy1 = dummy1.filter(teeth_list, &func);,是一个需要 int 的函数,但它没有得到。

有几个潜在的修复。

  1. 使用需要 Dummy 的函数(并自己提取牙齿 ;-))。
  2. 传递牙齿的数量(目前无法访问),而不是整个假人。
  3. Dummy 中提供 int 的转换运算符(大概返回齿数)也应该有效。

转换运算符方法似乎对我有用。我插入了

    operator int() { return num_of_teeth; }

进入 public 部分的 Dummy class。

这是否是好的风格值得商榷。可能出乎意料的是,Dummy 也是一个 int。也许还有一个论点认为谓词函数应该对整个节点数据起作用,但这值得商榷:可以处理所有 int-oid 的通用、可重用函数也有其优点。从 C++11 开始,您可以通过显式化来减轻转换为 int 的意外情况:它仍然是可能的,但需要静态转换。

至于剩下的代码:

  • 为列表定义一个合适的赋值:head = t.head;——一个浅拷贝,共享所有节点——导致在每个节点上双重删除当列表超出范围
  • 不要混用 malloc 和 delete
  • 首先不要使用裸指针,使用智能指针
  • 检查你的插入函数,逻辑似乎过于复杂并且可能有错误
  • Node 构造函数也应该为 null next
  • 您可能需要 const 迭代器。

确保为这样的容器编写广泛的测试class,在开头、结尾等处进行随意插入和删除。确保用空列表覆盖所有边缘情况。没有严格的测试,容器很难完全正确。