首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'void (ClassName::)(QString&)‘类型的参数与'void (ClassName::*)(QString&)’不匹配

'void (ClassName::)(QString&)‘类型的参数与'void (ClassName::*)(QString&)’不匹配
EN

Stack Overflow用户
提问于 2010-12-09 05:08:55
回答 3查看 1.6K关注 0票数 0

我正在尝试使用Qt的qtconcurrentmap来处理一些图像,并收到以下错误

代码语言:javascript
复制
argument of type 'void (ClassName::)(QString&)' does not match 'void (ClassName::*)(QString&)

我也得到了

代码语言:javascript
复制
/Library/Frameworks/QtCore.framework/Headers/qtconcurrentmapkernel.h:: 
In member function 'bool QtConcurrent::MapKernel<Iterator, MapFunctor>::runIteration(Iterator, int, void*) 
[with Iterator = QList<QString>::iterator, MapFunctor = void (ClassName::*)(QString&)]':


/Library/Frameworks/QtCore.framework/Headers/qtconcurrentmapkernel.h:73: 
error: must use '.*' or '->*' to call pointer-to-member function in 
'((QtConcurrent::MapKernel<QList<QString>::iterator, void (ClassName::*)(QString&)>*)this)->QtConcurrent::MapKernel<QList<QString>::iterator, void (ClassName::*)(QString&)>::map (...)'

这是我的代码

代码语言:javascript
复制
void ClassName::processImage(QString &f)
{

    Image image;
        image.read(qPrintable(f));
        try {
            //Apply effects on an Image
        } catch ( Magick::Exception & error) {
            // Displaying any possible errors on the text browser
            ui->textBrowser_error->setText(error.what());

        }
}



void ClassName::processAll() {

    foreach (QFileInfo f, list)
     {      QString str(f.absoluteFilePath());
            QList<QString> listof;
            listof.append(str);
     }


    QFuture<void> future = QtConcurrent::map(listof, processImage);
}

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-09 05:11:38

这里有两件事。首先,变量listof是在foreach循环中声明的,因此在创建future时可能看不到它。其次,您应该使用&ClassName::processImage作为QtConcurrent::map()的第二个参数。

更新

查看文档,您似乎需要以这种方式编写调用来创建映射:

代码语言:javascript
复制
QFuture<void> future = QtConcurrent::map(listof, boost::bind(&ClassName::processImage, this));

(您必须使用boost.bind将成员函数转换为普通函数,这就是map期望的第二个参数)。

票数 7
EN

Stack Overflow用户

发布于 2010-12-09 05:10:48

我认为它接受的是一个函数,而不是函数地址。

QFuture<void> future = QtConcurrent::map(listof, &ClassName::processImage);

票数 1
EN

Stack Overflow用户

发布于 2010-12-09 09:01:39

关于第二个错误:你不能像这样传递一个非静态成员函数。map()不知道在哪个对象上调用它(必须是ClassName类型)。

来自QtConcurrentMap文档:

QtConcurrent::map()、QtConcurrent::map()和QtConcurrent::mappedReduced()接受指向成员函数的指针。成员函数类类型必须与序列中存储的类型匹配:“

您传递了一个成员列表,但成员函数来自ClassName。您可以使用boost::bind (可能还可以使用STL中的mem_fun/bind1st fu)来实现类似的操作:

代码语言:javascript
复制
...map( listof, boost::bind( &ClassName::processImage, this, _1 ) );

这仍然不会起作用,因为您不能从UI线程以外的其他线程调用QTextEdit::setText()。

同样,它应该是processImage(const QString&)而不是processImage(QString&)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4392352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档