具有成员函数=的QtConcurrent :: map()无法编译 [英] QtConcurrent::map() with member function = can not compile
问题描述
我的项目是创建一个小程序,该程序演示搜索引擎的工作:为任意查询建立索引并返回结果.我已经完成了索引器部分的工作,现在我想通过一次索引多个文件来改进它. MainWindow类在这里:
My project is to create a small program which demonstrates the work of a search engine: indexing and returning result for arbitrary queries. I've done the work with the indexer part and now I want to improve it with indexing multiple files at once. The MainWindow class is here:
class MainWindow : public QMainWindow
{
Q_OBJECT
.....
private:
Indexer * indexer;
QStringList fileList;
....
void index(QStringList list);
void add(const QString &filename);
}
这是add
的实现(add
需要访问fileList
以避免再次索引同一文件,因此不能是静态方法):
This is the implementation of add
(add
need to access fileList
to avoid index the same files again, thus it can not be static method):
void MainWindow::add(const QString &filename)
{
if (!fileList.contains(filename))
{
indexer->addDocument(filename.toStdString());
fileList.append(filename);
qDebug() << "Indexed" << filename;
emit updatedList(fileList);
}
}
index
方法的实现是接收文件列表,并在每个文件名上调用add
:
The implement of index
method is to receive a file lists and call add
upon each file name:
void MainWindow::index(QStringList list)
{
....
QtConcurrent::map(list, &MainWindow::add);
....
}
我在编译这些代码时收到的错误是:
The error I receive when compiling these code is:
usr/include/qt4/QtCore/qtconcurrentmapkernel.h: In member function 'bool QtConcurrent::MapKernel<Iterator, MapFunctor>::runIteration(Iterator, int, void*) [with Iterator = QList<QString>::iterator, MapFunctor = QtConcurrent::MemberFunctionWrapper1<void, MainWindow, const QString&>]':
../search-engine/mainwindow.cpp:361:1: instantiated from here
/usr/include/qt4/QtCore/qtconcurrentmapkernel.h:73:9: error: no match for call to '(QtConcurrent::MemberFunctionWrapper1<void, MainWindow, const QString&>) (QString&)'
/usr/include/qt4/QtCore/qtconcurrentfunctionwrappers.h:128:7: note: candidate is:
/usr/include/qt4/QtCore/qtconcurrentfunctionwrappers.h:138:14: note: T QtConcurrent::MemberFunctionWrapper1<T, C, U>::operator()(C&, U) [with T = void, C = MainWindow, U = const QString&]
/usr/include/qt4/QtCore/qtconcurrentfunctionwrappers.h:138:14: note: candidate expects 2 arguments, 1 provided
我不太了解QtConcurrent的工作原理,并且文档中没有提供很多有关它的细节.我真的希望这里有人可以提供帮助.预先感谢.
I'm not really familiar with how QtConcurrent works, and the documentation doesn't provide much details about it. I really hope that someone here can help. Thanks in advance.
推荐答案
要能够调用指向成员的指针,除了需要使用函数的形式参数之外,还需要该类的实例(this
成员函数内部的指针).
To be able to call a pointer-to-member, you need, in addition to that functions formal arguments, an instance of that class (the this
pointer that you get inside member functions).
有两种处理方法:创建一个简单的函子来包装调用,或使用lambda.
There are two ways to handle this: create a simple functor to wrap the call, or use a lambda.
函子看起来像这样:
struct AddWrapper {
MainWindow *instance;
AddWrapper(MainWindow *w): instance(w) {}
void operator()(QString const& data) {
instance->add(data);
}
};
您将像这样使用它:
AddWrapper wrap(this);
QtConcurrent::map(list, wrap);
(但是请注意该包装器的生命周期.您可以使它更通用-例如,也可以在包装器中存储指向成员的指针,并且/或者如果要重用该结构,可以将其设为模板对于其他类型.)
(Careful with the lifetime of that wrapper though. You could make that more generic - you could also store a pointer-to-member in the wrapper for instance, and/or make it a template if you want to reuse that structure for other types.)
如果您具有带有lambdas的C ++ 11编译器,则可以避免所有这些样板:
If you have a C++11 compiler with lambdas, you can avoid all that boilerpalte:
QtConcurrent::map(list, [this] (QString const& data) { add(data); });
注意:我不确定QtConcurrent::MemberFunctionWrapper1
如何参与您的示例,在这里看不到.因此,在这种情况下,Qt中可能已经有一个通用包装器,但是我不知道.
Note: I'm not sure how QtConcurrent::MemberFunctionWrapper1
got involved in your example, I'm not seeing it here. So there might be a generic wrapper already in Qt for this situation, but I'm not aware of it.
这篇关于具有成员函数=的QtConcurrent :: map()无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!