C++ 如何处理此错误:从';布尔(*)(国际)和#x27;至';int';?
我正在为泛型排序列表编写这段代码,我必须编写一个过滤器方法,而不知道我将得到什么类型的参数。所以我这样写:C++ 如何处理此错误:从';布尔(*)(国际)和#x27;至';int';?,c++,templates,methods,sortedlist,C++,Templates,Methods,Sortedlist,我正在为泛型排序列表编写这段代码,我必须编写一个过滤器方法,而不知道我将得到什么类型的参数。所以我这样写: #include <iostream> #include <cstring> #include <string> #include <functional> #include "dummy.h" using namespace std; #ifndef SORT_H #define SORT_H template &
#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;
}
它编译但返回一个未知数字的无休止列表。。
像-
我在代码中的什么地方尝试访问禁止的内存?还是损坏的?调用
pred((curr->data)
pred
,从dummy1=dummy1.filter(tooth_list,&func);
这是一个需要int的函数,它没有得到int
有几个潜在的修复方法
Dummy
中的int
提供一个转换运算符(可能返回齿数)也应该可以工作 operator int() { return num_of_teeth; }
进入公共部分的Dummy
类
这是否是一种好的风格值得商榷。伪函数也是int可能出乎意料。可能还有一个论点认为谓词函数应该处理整个节点数据,但这是有争议的:一个可以处理int oid中所有内容的通用、可重用函数也有其优点。由于C++11,您可以通过使转换显式化来减轻转换为int
的意外:这仍然是可能的,但需要静态转换
至于代码的其余部分:
- 为列表定义适当的赋值:
head=t.head代码>-一个浅拷贝,共享所有节点-当列表超出范围时,会导致每个节点上的两次删除
- 不要混合使用malloc和delete
- 首先不要使用裸指针,使用智能指针
- 检查insert函数,逻辑似乎过于复杂,可能有缺陷
- 节点构造函数也应该为空
next
- 您可能需要常量迭代器
确保为这样一个容器类编写大量的测试,在开始、结束等处进行大量插入和删除。确保用空列表覆盖所有边缘情况。如果没有严格的测试,容器是很难完全正确的。即使不考虑显示代码和错误的问题,也会对容器进行升级;-)。请删除注释块,它们不需要重现错误,否?我在您的代码示例中找不到编译器抱怨的代码。“我遗漏了什么吗?”彼得:对不起,是的,我不小心复制了我试图修复时得到的错误。现在将其编辑为正确的错误。在这个阶段,如何使其工作可能过于雄心勃勃,让我们关注错误。这意味着过滤的第二个参数应该是某种函数。从逻辑上讲,它应该是一个接受T并返回bool的函数。它被宣布为什么?它是一个不相关类型B的值。如果向它提供
一个接受T并返回bool的函数,这可能会意外地起作用。你喂它什么?接受int并返回bool的函数。不,func应该有一个伪参数。我更改了函数,但现在它返回了一些废话,所以我想我有一些内存问题。。你可能看到问题了吗?它也在错误中。。。我没有调查它,但似乎你正在返回一个对某个临时地方的引用。@SpaceNugget在调试整个事情之前,你会一直更改问题的含义吗?您是否需要接受的答案来解决问题的所有迭代?(提示:对于一个不同的问题,你应该打开一个新的问题)@dratenik该问题似乎在25分钟前被编辑过,修正了一个编辑错误。我认为OP在这里是在请求一个简单的帮助,欢迎他这么做。@Peter,谢谢
====================[ 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
bool func(Dummy num) {
int number = num.get();
if (number % 2 != 0) {
return false;
}
return true;
}
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)
operator int() { return num_of_teeth; }